我需要合并两个数组
例如:
tcp://localhost:3000
我需要合并这些两个数组。我在每个主菜单下有2个子菜单。根据用户选择,我需要为主菜单添加什么以及添加所有子菜单
这是我工作的示例图片
这是我的代码,但它不起作用。
tcp://0.0.0.0:3000
我想要一个像这样的输出
Array( [menu_id] =>1,
[functions] => Array
(
[0] => AE,
[1] => AR
))
Array( [menu_id] =>2,
[functions] => Array
(
[0] => AE,
[1] => AR
)
)
但此代码输出为
if(isset($_POST['add_menu1']))
{
$arrayOrgPri = array("id"=>"1","functions"=>array("AE"));
}
if(isset($_POST['app_menu1']))
{
$newarry = array("id"=>"1","functions"=>array("AR"));
$arrayPrivilege = array_merge_recursive($arrayOrgPri,$newarry);
}
if(isset($_POST['add_menu2']))
{
$newarry = array("id"=>"2","functions"=>array("AE"));
$arrayPrivilege =array_merge_recursive($arrayPrivilege,$newarry);
}
if(isset($_POST['app_menu2']))
{
$newarry = array("id"=>"2","functions"=>array("AR"));
$arrayPrivilege = array_merge_recursive($arrayPrivilege,$newarry);
}
我如何使用php实现这一点 谢谢
答案 0 :(得分:0)
创建两个单独的数组并将两者结合起来就是解决方案
if(isset($_POST['add_menu1']))
{
$arrayOrgPri = array("id"=>"1","functions"=>array("AE"));
}
if(isset($_POST['app_menu1']))
{
$newarry = array("functions"=>array("AR"));
$arrayOrgPri = array_merge_recursive($arrayOrgPri,$newarry);
}
if(isset($_POST['add_menu2']))
{
$arrayPrivilege = array("id"=>"2","functions"=>array("AE"));
}
if(isset($_POST['app_menu2']))
{
$newarry = array("functions"=>array("AR"));
$arrayPrivilege = array_merge_recursive($arrayPrivilege,$newarry);
}
$combinearray = array($arrayOrgPri,$arrayPrivilege);
答案 1 :(得分:0)
您希望多个数组元素具有相同的键。这是不可能的。请改为使用以下结构:
` $ menu = array(
[1] => array(
[0] => AE,
[1] => AR,
),
[2] => array(
[0] => AE,
[1] => AR,
),
[another_menu_I'd] => array_with_functions
`