问题
I am working with a multidimensional array I want to be able to remove an array (and all children) which match an id.
The function I have tried is:
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;
}
My example array is:
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
)
)
)
)
and the result I am looking for (calling the function with (2, $array, 'children')
) is:
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
)
)
)
)
but the result I am getting is
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
)
)
)
I have no idea what's going on here!
回答1:
You can ease things by only using references.
function removeKey($key, &$array, $childKey = 'children'){
if(isset($array[$key])){
unset($array[$key]);
return;
}
foreach($array as &$item)
if(isset($item[$childKey]))
removeKey($key, $item[$childKey], $childKey);
}
Example:
$arr = array(...);
removeKey('key', $arr, $chilKey);
// Just continue using $arr
回答2:
What about multidimensional array? I was researched for a couple of hours for this solution, nowhere found an optimal solution. so, i wrote it by myself
function allow_keys($arr, $keys)
{
$saved = [];
foreach ($keys as $key => $value) {
if (is_int($key) || is_int($value)) {
$keysKey = $value;
} else {
$keysKey = $key;
}
if (isset($arr[$keysKey])) {
$saved[$keysKey] = $arr[$keysKey];
if (is_array($value)) {
$saved[$keysKey] = allow_keys($saved[$keysKey], $keys[$keysKey]);
}
}
}
return $saved;
}
use: example
$array = [
'key1' => 'kw',
'loaa'=> ['looo'],
'k' => [
'prope' => [
'prop' => ['proo', 'prot', 'loolooo', 'de'],
'prop2' => ['hun' => 'lu'],
],
'prop1' => [
],
],
];
call: example
allow_keys($array, ['key1', 'k' => ['prope' => ['prop' => [0, 1], 'prop2']]])
output:
Array ( [key1] => kw [k] => Array ( [prope] => Array ( [prop] => Array ( [0] => proo [1] => prot ) [prop2] => Array ( [hun] => lu ) ) ) )
so you get only needed keys from the multidimensional array. it is not limited only for "multidimensional", you can use it by passing an array like
['key1', 'loaa']
output you get:
Array ( [key1] => kw [loaa] => Array ( [0] => looo ) )
hope someone helps this one, as i searched a lot, and nothing found. cheers!
来源:https://stackoverflow.com/questions/12053433/recursive-search-and-remove-in-array