php数组推送行为
我是php新手,不知道为什么这不起作用。有人能帮我吗?谢谢我的代码如下:php数组推送行为,php,arrays,multidimensional-array,array-push,Php,Arrays,Multidimensional Array,Array Push,我是php新手,不知道为什么这不起作用。有人能帮我吗?谢谢我的代码如下: if (!$this->_in_multinested_array($over_time, $request_year)) { $temp = array('name'=>$request_year, 'children'=>array()); array_push($over_time, $temp); } if (!$t
if (!$this->_in_multinested_array($over_time, $request_year)) {
$temp = array('name'=>$request_year, 'children'=>array());
array_push($over_time, $temp);
}
if (!$this->_in_multinested_array($over_time, $request_month)) {
$child = array('name'=>$request_month, 'children'=>array());
foreach ($over_time as $temp) {
if ($temp['name'] == $request_year) {
array_push($temp['children'], $child);
}
}
}
每当我检查此代码的结果时,
temp['children']
数组总是空的,即使它不应该是空的。此循环中的每个$temp都是一个副本:
foreach ($over_time as $temp) {
if ($temp['name'] == $request_year) {
array_push($temp['children'], $child);
}
}
您希望更改数组而不是制作副本,因此必须使用引用:
foreach ($over_time as &$temp) {
if ($temp['name'] == $request_year) {
array_push($temp['children'], $child);
}
}
是的,对不起,打字错误!我的代码中有一个$。@MarsJ因为两个数组都是空的,所以您得到的是空的$temp['children']肯定是空数组,尽管我想用$child数组填充它。但这些不是空的