我有兩個條件price_id =2
和price_id = 1
的數據和下面的數據。 Foreach循環給出了錯誤的結果
所以在這裏,如果我的條件weekday_am
列的值是在price_id =2
null
或empty
或0.00
我需要把該值在price_id = 1
和同樣的邏輯適用於列weekday_pm
,weekend_am
,weekend_pm
。所以我在做什麼是我已經寫了兩個不同的條件查詢和使用foreach循環檢查值。但它給出了錯誤的結果。
查詢代碼:
$ArrayTier = BundlePrice::where('bundle_id',$id)->where('price_id','=','2')->get();
$ArrayDefault = BundlePrice::where('bundle_id',$id)->where('price_id','=','1')->get();
foreach($ArrayTier as $value)
{
$bundle_id = $value->bundle_id;
$asset_id = $value->asset_id;
foreach($ArrayDefault as $v)
{
if(!empty($value->weekday_am) || ($value->weekday_am != null))
{
$weekam = $value->weekday_am;
}else{
$weekam = $v->weekday_am;
}
if(!empty($value->weekday_pm) || ($value->weekday_pm != null))
{
$weekpm = $value->weekday_pm;
}else{
$weekpm = $v->weekday_pm;
}
if(!empty($value->weekend_am) || ($value->weekend_am != null))
{
$nonweekam = $value->weekend_am;
}else{
$nonweekam = $v->weekend_am;
}
if(!empty($value->weekend_pm) || ($value->weekend_pm != null))
{
$nonweekpm = $value->weekend_pm;
}else{
$nonweekpm = $v->weekend_pm;
}
}
$primaryArray[] = ['asset_id' => $asset_id,'weekam' => $weekam,'weekpm' => $weekpm,'nonweekam' => $nonweekam,'nonweekpm' => $nonweekpm];
}
以上查詢我越來越像錯誤的值不會在price_id =1
結果改變其只取最後一行值上面的代碼是:
array:3 [▼
0 => array:5 [▼
"asset_id" => 2
"weekam" => 150.0
"weekpm" => 320.0
"nonweekam" => 160.0
"nonweekpm" => 420.0
]
1 => array:5 [▼
"asset_id" => 1
"weekam" => 120.0
"weekpm" => 180.0
"nonweekam" => 220.0
"nonweekpm" => 420.0
]
2 => array:5 [▼
"asset_id" => 4
"weekam" => 120.0
"weekpm" => 320.0
"nonweekam" => 220.0
"nonweekpm" => 420.0
]
]
如果您看到上述內容,則對於少數地方重複相同的值。 但預期結果應該象下面這樣:
array:3 [▼
0 => array:5 [▼
"asset_id" => 2
"weekam" => 150.0
"weekpm" => 300.0
"nonweekam" => 160.0
"nonweekpm" => 400.0
]
1 => array:5 [▼
"asset_id" => 1
"weekam" => 110.0
"weekpm" => 180.0
"nonweekam" => 210.0
"nonweekpm" => 410.0
]
2 => array:5 [▼
"asset_id" => 4
"weekam" => 120.0
"weekpm" => 320.0
"nonweekam" => 220.0
"nonweekpm" => 420.0
]
]
有人可以幫我解決呢?或者請向我推薦任何可以在單個查詢中執行此操作的mysql查詢,而不是通過foreach循環。謝謝
我沒有得到你的整個代碼,但我想你試圖檢查 'if(notEmpty and notNULL)'。因此,使用'&&'運算符而不是'||'運算符 –
預期的結果與您看到的結果或多或少相同。我沒有看到任何重複值 – apokryfos
@apokryfos:請檢查兩個數組中的'nonweekpm'值,您將會知道我想要解釋的內容 – 06011991