我有这个代码:
<?php
foreach($arrs_menu as $show_submenu => $k){
$get_parent_menus[$k['donemen']][] = array(
$k['id_page'] => $k['id_page']
);
}
?>
这里的问题是,我正在尝试在$get_parent_menus[$k['donemen']]
中放置一个新数组,因此当我放置此[]
时,为了不替换当前数组并创建一个新数组,它给了我一个错误:[]
运算符不支持字符串。
如果我删除它,我可以正常,但只有一次正确设置了值。
这段代码有什么问题?
似乎$get_parent_menus[$k['donemen']]
是一个字符串而不是数组。
你可以做的是,如果原始值已经不是数组,就把它放在数组中:
if (!is_array($get_parent_menus[$k['donemen']]))
{
// trying to keep the original value although I don't know where the key should come from...
$get_parent_menus[$k['donemen']] = array(array('some_key' => $get_parent_menus[$k['donemen']]));
}
$get_parent_menus[$k['donemen']][] = array($k['id_page'] => $k['id_page']);