samedi 31 juillet 2021

Multidimensional array inside foreach loop

I have this Category List where i'm creating menu planner this code is working and the output of this code is given below,

 $categoryList=Category::where('user_id',$id)->get();   
    $meal_day=array('1'=>'Monday','2'=>'Tuesday','3'=>'Wednesday','4'=>'Thursday','5'=>'Friday','6'=>'Saturday','7'=>'Sunday');
    $meal_plan=[];
    $i= 0;
    foreach($categoryList as $catKey => $row)
    {  
        for($k=1;$k<=count($meal_day);$k++)
        {   
            $menuPlanner=MenuPlanner::where(['day'=>$k,'user_id'=>auth()->user()->id,'category_id'=>$row['id']])->first(); 
            if($menuPlanner)
            {
                $product = Product::where(['id'=>$menuPlanner->product_id])->first();
                $meal_plan[$catKey][$k]['product_title']=$product->title;
                
            }
        }
    }

Output:

array:2 [
  0 => array:7 [
    1 => array:1 [
      "product_title" => "Product 1"
    ]
    2 => array:1 [
      "product_title" => ""
    ]
    3 => array:1 [
      "product_title" => ""
    ]
    4 => array:1 [
      "product_title" => ""
    ]
    5 => array:1 [
      "product_title" => ""
    ]
    6 => array:1 [
      "product_title" => ""
    ]
    7 => array:1 [
      "product_title" => ""
    ]
  ]
  1 => array:7 [
    1 => array:1 [
      "product_title" => "Product 2"
    ]
    2 => array:1 [
      "product_title" => ""
    ]
    3 => array:1 [
      "product_title" => ""
    ]
    4 => array:1 [
      "product_title" => ""
    ]
    5 => array:1 [
      "product_title" => ""
    ]
    6 => array:1 [
      "product_title" => ""
    ]
    7 => array:1 [
      "product_title" => ""
    ]
  ]

but i have multiple products in menu planner in the first

 $menuPlanner=MenuPlanner::where(['day'=>$k,'user_id'=>auth()->user()->id,'category_id'=>$row['id']])->get(); 
       
 foreach($menuPlanner as $menuKey => $menurow)
 {
 $product = Product::where(['id'=>$menurow->product_id])->first();
 $meal_plan[$catKey][$k]['product_title']=$product->title;
 }

So how will i store multiple product at array indexes.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire