繁体   English   中英

Foreach循环给出错误的结果

[英]Foreach loop is giving wrong results

我有两个条件price_id =2price_id = 1表数据,数据如下。 在此处输入图片说明

因此,在这里我的条件是,如果weekday_am列的值为null或为empty或者在price_id =2 0.00 ,则我需要在price_id = 1采用该值,并且相同的逻辑将适用于weekday_pmweekend_amweekend_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循环。 谢谢

您忘记了加入资产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];        
}

看起来也比较干净。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM