递归搜索并在数组中删除?
Posted
技术标签:
【中文标题】递归搜索并在数组中删除?【英文标题】:Recursive search and remove in array? 【发布时间】:2012-08-16 17:08:36 【问题描述】:我正在处理一个多维数组,我希望能够删除与 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
)
)
)
我不知道这里发生了什么!
【问题讨论】:
【参考方案1】:多维数组呢?我为这个解决方案研究了几个小时,没有找到最佳解决方案。所以,我自己写的
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;
使用:示例
$array = [
'key1' => 'kw',
'loaa'=> ['looo'],
'k' => [
'prope' => [
'prop' => ['proo', 'prot', 'loolooo', 'de'],
'prop2' => ['hun' => 'lu'],
],
'prop1' => [
],
],
];
调用:示例
allow_keys($array, ['key1', 'k' => ['prope' => ['prop' => [0, 1], 'prop2']]])
输出:
Array ( [key1] => kw [k] => Array ( [prope] => Array ( [prop] => Array ( [0] => proo [1] => prot ) [prop2] => Array ( [hun] => lu ) ) ) )
所以你从多维数组中只得到需要的键。它不仅限于“多维”,您可以通过传递类似的数组来使用它
['key1', 'loaa']
你得到的输出:
Array ( [key1] => kw [loaa] => Array ( [0] => looo ) )
希望有人帮助这个,因为我搜索了很多,但一无所获。 干杯!
【讨论】:
也许这不是这个问题的直接答案,但是当您通过问题“递归搜索并删除数组”对 google 进行研究时,您会发现这些主题不回答以正确的方式提出这个问题。 请不要转发您的答案。您应该编辑您的答案以适合问题。【参考方案2】:您可以通过仅使用参考来简化事情。
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);
例子:
$arr = array(...);
removeKey('key', $arr, $chilKey);
// Just continue using $arr
【讨论】:
干杯,这行得通,但是我实际上做错了什么才能得到上述输出? 老实说并没有检查,但是您将 $item 节点(包含“子”节点的节点)替换为已清理的数组。但是,使用引用应该容易得多。以上是关于递归搜索并在数组中删除?的主要内容,如果未能解决你的问题,请参考以下文章