Php Foreach循环给出了错误的结果
我有两个条件的表数据Php Foreach循环给出了错误的结果,php,mysql,arrays,laravel,foreach,Php,Mysql,Arrays,Laravel,Foreach,我有两个条件的表数据price\u id=2和price\u id=1,数据如下所示。 因此,这里我的条件是,如果weekday\u am列值为null或empty或0.00在price\u id=2处,我需要在price\u id=1处取该值,并且相同的逻辑将应用于列weekday\u pm,weekend\u am,。所以我现在做的是用两种不同的条件编写两个查询,并使用foreach循环检查值。但它给出了错误的结果 查询代码: $ArrayTier = BundlePrice::where
price\u id=2
和price\u id=1
,数据如下所示。
因此,这里我的条件是,如果weekday\u am
列值为null
或empty
或0.00
在price\u id=2
处,我需要在price\u id=1
处取该值,并且相同的逻辑将应用于列weekday\u pm
,weekend\u am
,。所以我现在做的是用两种不同的条件编写两个查询,并使用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\u 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循环。谢谢你你忘了加入资产ID,所以你在循环时会胡言乱语。这样做:
$prices = BundlePrice::where('bundle_id',$id)->get()->groupBy("asset_id"); //The group by is done on the collection, not the query: https://laravel.com/docs/5.4/collections#method-groupby
foreach($prices as $bundleprice) {
$price = collect($bundleprice)->where("price_id",1)->first();
$default = collect($bundleprice)->where("price_id",2)->first();
if (empty($price) && empty($default)) { continue; }
if (empty($default)) {
$default = $price;
}
$weekam = !empty($price->weekday_am)?$price->weekday_am:$default->weekday_am;
$weekpm = !empty($price->weekday_pm)?$price->weekday_pm:$default->weekday_pm;
$nonweekam = !empty($price->weekend_am)?$price->weekend_am:$default->weekend_am;
$nonweekpm = !empty($price->weekend_pm)?$price->weekend_pm:$default->weekend_pm;
$primaryArray[] = ['asset_id' => $default->asset_id,'weekam' => $weekam,'weekpm' => $weekpm,'nonweekam' => $nonweekam,'nonweekpm' => $nonweekpm];
}
看起来也有点干净。您忘记加入资产ID,因此在循环时会变得乱七八糟。这样做:
$prices = BundlePrice::where('bundle_id',$id)->get()->groupBy("asset_id"); //The group by is done on the collection, not the query: https://laravel.com/docs/5.4/collections#method-groupby
foreach($prices as $bundleprice) {
$price = collect($bundleprice)->where("price_id",1)->first();
$default = collect($bundleprice)->where("price_id",2)->first();
if (empty($price) && empty($default)) { continue; }
if (empty($default)) {
$default = $price;
}
$weekam = !empty($price->weekday_am)?$price->weekday_am:$default->weekday_am;
$weekpm = !empty($price->weekday_pm)?$price->weekday_pm:$default->weekday_pm;
$nonweekam = !empty($price->weekend_am)?$price->weekend_am:$default->weekend_am;
$nonweekpm = !empty($price->weekend_pm)?$price->weekend_pm:$default->weekend_pm;
$primaryArray[] = ['asset_id' => $default->asset_id,'weekam' => $weekam,'weekpm' => $weekpm,'nonweekam' => $nonweekam,'nonweekpm' => $nonweekpm];
}
看起来也有点干净。我不明白你的全部代码,但我想你是在尝试检查是否(notEmpty和notNULL)
。因此,请使用&&
运算符而不是|
运算符。在我看来,预期结果与您得到的结果大致相同。我没有看到任何重复values@apokryfos:请检查两个数组中的nonweekpm
值。您将了解我试图解释的内容。我不了解您的全部代码,但我认为您正在尝试检查是否(notEmpty和notNULL)
。因此,请使用&&
运算符而不是|
运算符。在我看来,预期结果与您得到的结果大致相同。我没有看到任何重复values@apokryfos:请检查两个数组中的nonweekpm
值,您将了解我试图解释的内容