Home >Backend Development >PHP Tutorial > foreach 赋值 二维数组解决思路

foreach 赋值 二维数组解决思路

WBOY
WBOYOriginal
2016-06-13 13:21:31798browse

foreach 赋值 二维数组
foreach ($menu as $k=>$v){
$arr[]['mid'] = $k;
$arr[]['num'] = $v;
}
print_r($arr);
输出:
Array ( [0] => Array ( [mid] =>9999 ) [1] => Array ( [num] => 1 ) ) 
而不是
Array ( [0] => Array ( [mid] =>9999 [num] => 1 ) )??

为什么? 请教啊。 跟我想的不太一样,想不通啊,现在还在线。。。

------解决方案--------------------

PHP code
foreach ($menu as $k=>$v){
     $arr[]=array('mid' =>$k, 'num' =>$v);
}
<br><font color="#e78608">------解决方案--------------------</font><br>
探讨

PHP code
foreach ($menu as $k=>$v){
$arr[]=array('mid' =>$k, 'num' =>$v);
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn