2017-07-18 134 views
0

我有兩個條件price_id =2price_id = 1的數據和下面的數據。 enter image description hereForeach循環給出了錯誤的結果

所以在這裏,如果我的條件weekday_am列的值是在price_id =2nullempty0.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循環。謝謝

+0

我沒有得到你的整個代碼,但我想你試圖檢查 'if(notEmpty and notNULL)'。因此,使用'&&'運算符而不是'||'運算符 –

+0

預期的結果與您看到的結果或多或少相同。我沒有看到任何重複值 – apokryfos

+0

@apokryfos:請檢查兩個數組中的'nonweekpm'值,您將會知道我想要解釋的內容 – 06011991

回答

2

你忘記加入資產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];   
} 

看起來也有點乾淨。

+0

非常感謝:)它工作正常 – 06011991