[英]Generate a multidimensional array with foreach loop
I'am stuck and didn't found any tutorial or examples on how to produce the multidimensional array like this while using foreach()
: 我遇到了困难,并且在使用foreach()
时没有找到关于如何生成这样的多维数组的任何教程或示例:
'0' => '-- ALL --',
'CATEGORY 1' => array(
'11' => 'Item 11',
'12' => 'Item 12',
'13' => 'Item 13',
),
'CATEGORY 2' => array(
'14' => 'Item 14',
'15' => 'Item 15',
'16' => 'Item 16',
)
And this is where I'am stucked: 这就是我被困的地方:
$items = $this->model->get_categories();
foreach($items as $item){
$result[$item->title] = array();
// HOW TO CONTINUE NEXT ? :(
}
Something like 就像是
$items = $this->model->get_categories();
foreach($items as $item){
$result[$item->title] = array();
foreach($item->data as $key => $data){ //replace $item->data with whatever your second level stuff is
$result[$item->title][$key] = $data;
}
}
Assuming the value is in $item->value: 假设值在$ item-> value中:
$result[$item->title][] = $item->value; $ result [$ item-> title] [] = $ item-> value;
will append $item->value to your array. 将$ item-> value附加到您的数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.