PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, March 7, 2022

[FIXED] Multidimensional array inside foreach loop

 March 07, 2022     laravel, laravel-5, php     No comments   

Issue

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 the 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 products at array indexes?


Solution

The index ['product_title'] has to be treated as an array, and in that array, the product titles has to be pushed in using array_push.

In this way you can have multiple product titles.

Look at the following example:

<?php
    
    $menuPlanner=MenuPlanner::where(['day'=>$k,'user_id'=>auth()->user()->id,'category_id'=>$row['id']])->get(); 
    
    //DECLARE THE INDEX AS AN ARRAY
    $meal_plan[$catKey][$k]['product_title'] = array();
    
    foreach($menuPlanner as $menuKey => $menurow)
    {
        $product = Product::where(['id'=>$menurow->product_id])->first();
        
        //PUSH TITLE INTO THE ARRAY
        array_push($meal_plan[$catKey][$k]['product_title'], $product->title);
        
    }
    
?>


Answered By - yuko
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing