我有一个多维数组我希望能够删除阵列(和所有儿童)匹配的ID,其工作。
我已经试过的功能是:
function removeKey($key, $array, $childKey = 'children'){
if(isset($array[$key])){
unset($array[$key]);
return $array;
}
foreach($array as &$item)
if(isset($item[$childKey]))
$item = removeKey($key, $item[$childKey], $childKey);
return $array;
}
我的示例性阵列是:
Array
(
[5] => Array
(
[id] => 5
[parent_id] =>
[menu_title] => Drinks
[page_title] => Drinks
[status] => 1
[products] => 0
)
[1] => Array
(
[id] => 1
[parent_id] =>
[menu_title] => Electronics
[page_title] => Electronics
[status] => 1
[products] => 0
[children] => Array
(
[2] => Array
(
[id] => 2
[parent_id] => 1
[menu_title] => Computers
[page_title] => Computers
[status] => 1
[products] => 0
[children] => Array
(
[4] => Array
(
[id] => 4
[parent_id] => 2
[menu_title] => Apple
[page_title] => Apple - Imacs and Macbooks
[status] => 1
[products] => 0
)
)
)
[3] => Array
(
[id] => 3
[parent_id] => 1
[menu_title] => Mobile Phones
[page_title] => Mobile Phones
[status] => 1
[products] => 0
)
)
)
)
和我期待的结果(调用带有功能(2, $array, 'children')
是:
Array
(
[5] => Array
(
[id] => 5
[parent_id] =>
[menu_title] => Drinks
[page_title] => Drinks
[status] => 1
[products] => 0
)
[1] => Array
(
[id] => 1
[parent_id] =>
[menu_title] => Electronics
[page_title] => Electronics
[status] => 1
[products] => 0
[children] => Array
(
[3] => Array
(
[id] => 3
[parent_id] => 1
[menu_title] => Mobile Phones
[page_title] => Mobile Phones
[status] => 1
[products] => 0
)
)
)
)
但我得到的结果是
Array
(
[5] => Array
(
[id] => 5
[parent_id] =>
[menu_title] => Drinks
[page_title] => Drinks
[status] => 1
[products] => 0
)
[1] => Array
(
[3] => Array
(
[id] => 3
[parent_id] => 1
[menu_title] => Mobile Phones
[page_title] => Mobile Phones
[status] => 1
[products] => 0
)
)
)
我不知道这是怎么回事就在这里!