如何用这种格式区分数组?
原始数组:
Dump => array(3) {
[1] => string(9) "Dashboard"
["Pages"] => array(2) {
[19] => string(25) "Menu1"
[10022] => string(21) "Menu2"
}
["Users"] => array(5) {
[19] => string(25) "Menu1"
[10022] => string(21) "Menu2"
[10019] => string(11) "Menu3"
[10020] => string(12) "Menu4"
[10021] => string(18) "Menu5"
}
}
我想动态删除键19
和10022
,因为它已存在于关键页面上。
预期结果:
Dump => array(3) {
[1] => string(9) "Dashboard"
["Pages"] => array(2) {
[19] => string(25) "Menu1"
[10022] => string(21) "Menu2"
}
["Users"] => array(5) {
[10019] => string(11) "Menu3"
[10020] => string(12) "Menu4"
[10021] => string(18) "Menu5"
}
}
答案 0 :(得分:2)
试试这个:
$array = ....;
foreach ($array['Pages'] as $k => $v)
if (isset($array['Users'][$k]))
unset($array['Users'][$k]);
答案 1 :(得分:0)
您可以使用 array_diff_key 功能:
$Array = array(
1 => 'Dashboard',
'Pages' => array(
19 => 'Menu1',
10022 => 'Menu2'
),
'Users' => array(
19 => 'Menu1',
10022 => 'Menu2',
10019 => 'Menu3',
10020 => 'Menu4',
10021 => 'Menu5'
)
);
$Array['Users'] = array_diff_key($Array['Users'], $Array['Pages']);
var_dump($Array);
答案 2 :(得分:0)
试试这个......
$array = array(1=>"Dashboard",
"Pages"=>array(
19 =>"Menu1"
10022=>"Menu2"
),
"Users" => array(10019=>"Menu3"
10020 =>"Menu4"
10020=>"Menu5"))
foreach($array["Pages"] as $k=>$v){
if(array_key_exists($k, $array["Users"])){
unset($array["Users"][$k]);
}
}