foreach 循环内的多维数组
Multidimensional array inside foreach loop
我有这个类别列表,我正在其中创建菜单规划器这段代码正在运行,下面给出了这段代码的输出,
$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;
}
}
}
输出:
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" => ""
]
]
但我在第一个菜单计划器中有多个产品
$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;
}
那么如何在数组索引中存储多个产品?
必须将索引 ['product_title']
视为一个数组,并且在该数组中,必须使用 array_push
.
推入产品标题
这样您就可以拥有多个个产品标题。
看下面的例子:
<?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);
}
?>
我有这个类别列表,我正在其中创建菜单规划器这段代码正在运行,下面给出了这段代码的输出,
$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;
}
}
}
输出:
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" => ""
]
]
但我在第一个菜单计划器中有多个产品
$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;
}
那么如何在数组索引中存储多个产品?
必须将索引 ['product_title']
视为一个数组,并且在该数组中,必须使用 array_push
.
这样您就可以拥有多个个产品标题。
看下面的例子:
<?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);
}
?>