问题描述
我有一个基于以下数组的嵌套树结构:
<前>大批([1] => 数组([id] => 1[父] => 0[名称] => 起始页[uri] => 125[basename] => index.php[孩子] =>)[23] => 数组([id] => 23[父] => 0[名称] => 事件[uri] => 0[基名] =>[子] => 数组([24] => 数组([id] => 24[父] => 23[名称] => 公共新闻[uri] => 0[基名] =>[子] => 数组([27] => 数组([id] => 27[父] => 24[名称] => 添加[uri] => 100[basename] => news.public.add.php[孩子] =>)[28] => 数组([id] => 28[父] => 24[名称] => 概述[uri] => 101[basename] => news.public.overview.php[孩子] =>)))[25] => 数组([id] => 25[父] => 23[名称] => 私人新闻[uri] => 0[基名] =>[子] => 数组([29] => 数组([id] => 29[父] => 25[名称] => 添加[uri] => 67[basename] => news.private.add.php[孩子] =>)[30] => 数组([id] => 30[父] => 25[名称] => 概述[uri] => 68[basename] => news.private.overview.php[孩子] =>)))[26] => 数组([id] => 26[父] => 23[名称] => 日历[uri] => 0[基名] =>[子] => 数组([31] => 数组([id] => 31[父] => 26[名称] => 添加[uri] => 69[basename] => news.event.add.php[孩子] =>)[32] => 数组([id] => 32[父] => 26[名称] => 概述[uri] => 70[basename] => news.event.overview.php[孩子] =>))))))我正在寻找一个函数来循环(递归?)数组并删除一些键.
我的系统我可以允许用户访问某些功能/页面,如果我拒绝访问整个阻止"事件",数组将如下所示:
数组 (1 =>大批 ('id' => '1','父母' => '0','name' => '起始页','uri' => '125','basename' => 'index.php','孩子' => '',),23 =>大批 ('id' => '23','父母' => '0','name' => '事件','uri' => '0','basename' => NULL,'孩子' =>大批 (24 =>大批 ('id' => '24','父母' => '23','name' => '公共新闻','uri' => '0','basename' => NULL,'孩子' => '',),25 =>大批 ('id' => '25','父母' => '23','name' => '私人新闻','uri' => '0','basename' => NULL,'孩子' => '',),26 =>大批 ('id' => '26','父母' => '23','name' => '日历','uri' => '0','basename' => NULL,'孩子' => '',),),))
正如您在上面看到的,整个块"事件"现在没有用,因为没有与每个选项相关联的页面.所以我需要找到所有keys",其中basename"为空,并且 child 不是数组或数组为空,然后删除它们.我在搜索网站时发现了这个功能:
function searchAndDestroy(&$a, $key, $val){foreach($a as $k => &$v){if(is_array($v)){$r = searchAndDestroy($v, $key, $val);如果($r){未设置($a[$k]);}} elseif ($key == $k && $val == $v) {返回真;}}返回假;}
它可用于删除数组中任何位置的键,但仅基于一件事,例如删除父"等于23"的所有键.但是我需要查找并删除(取消设置)basename"为空且 child 不是数组或数组为空的所有键.任何人都可以帮助我并可能调整上面的功能吗?
谢谢,
解决了!将此函数添加到我的类中:
private function cleanTree(&$arr){foreach($arr as $key => &$item) {if(!$item["child"] && empty($item["basename"])){未设置($arr[$key]);}elseif(is_array($item["child"])){if(count($item["child"]) == 0){取消设置($arr[$item["id"]]);}别的{$this->cleanTree($item["child"]);}}}
}
要删除 ROOT 级别以及其他任何人上不必要的元素,只需运行以上两次即可.
I've got a nested tree structure which is based on the array below:
Array ( [1] => Array ( [id] => 1 [parent] => 0 [name] => Startpage [uri] => 125 [basename] => index.php [child] => ) [23] => Array ( [id] => 23 [parent] => 0 [name] => Events [uri] => 0 [basename] => [child] => Array ( [24] => Array ( [id] => 24 [parent] => 23 [name] => Public news [uri] => 0 [basename] => [child] => Array ( [27] => Array ( [id] => 27 [parent] => 24 [name] => Add [uri] => 100 [basename] => news.public.add.php [child] => ) [28] => Array ( [id] => 28 [parent] => 24 [name] => Overview [uri] => 101 [basename] => news.public.overview.php [child] => ) ) ) [25] => Array ( [id] => 25 [parent] => 23 [name] => Private news [uri] => 0 [basename] => [child] => Array ( [29] => Array ( [id] => 29 [parent] => 25 [name] => Add [uri] => 67 [basename] => news.private.add.php [child] => ) [30] => Array ( [id] => 30 [parent] => 25 [name] => Overview [uri] => 68 [basename] => news.private.overview.php [child] => ) ) ) [26] => Array ( [id] => 26 [parent] => 23 [name] => Calendar [uri] => 0 [basename] => [child] => Array ( [31] => Array ( [id] => 31 [parent] => 26 [name] => Add [uri] => 69 [basename] => news.event.add.php [child] => ) [32] => Array ( [id] => 32 [parent] => 26 [name] => Overview [uri] => 70 [basename] => news.event.overview.php [child] => ) ) ) ) ) )
I'm looking for a function to loop (recursive?) through the array and remove some keys.
I my system I can allow users to certain functions/pages and if I deny access to the whole "block" "Events", the array will look like this:
array ( 1 => array ( 'id' => '1', 'parent' => '0', 'name' => 'Start page', 'uri' => '125', 'basename' => 'index.php', 'child' => '', ), 23 => array ( 'id' => '23', 'parent' => '0', 'name' => 'Events', 'uri' => '0', 'basename' => NULL, 'child' => array ( 24 => array ( 'id' => '24', 'parent' => '23', 'name' => 'Public news', 'uri' => '0', 'basename' => NULL, 'child' => '', ), 25 => array ( 'id' => '25', 'parent' => '23', 'name' => 'Private news', 'uri' => '0', 'basename' => NULL, 'child' => '', ), 26 => array ( 'id' => '26', 'parent' => '23', 'name' => 'Calendar', 'uri' => '0', 'basename' => NULL, 'child' => '', ), ), ) )
As you can see above, the whole "block" "Events" is useless right now, becuase there is no page associated with each option. So I need to find all "keys" where "basename" is null AND where child is not an array or where the array is empty and remove them. I found this function when searching the site:
function searchAndDestroy(&$a, $key, $val){
foreach($a as $k => &$v){
if(is_array($v)){
$r = searchAndDestroy($v, $key, $val);
if($r) {
unset($a[$k]);
}
} elseif ($key == $k && $val == $v) {
return true;
}
}
return false;
}
It can be used to remove a key any where in the array, but only based in one thing, for example remove all keys where "parent" equals "23". But I need to find and remove (unset) all keys where "basename" is null AND where child isn't an array or where the array is empty. Can anyone help me out and possibly tweak the function above?
Thank you,
Solved it! Added this function to my class:
private function cleanTree(&$arr){ foreach($arr as $key => &$item) { if(!$item["child"] && empty($item["basename"])){ unset($arr[$key]);}elseif(is_array($item["child"])){ if(count($item["child"]) == 0){ unset($arr[$item["id"]]); }else{ $this->cleanTree($item["child"]); } } }
}
To remove unnecessary elements on the ROOT level(s) as well as anyone else, just run the above twice.
这篇关于循环遍历多维数组并删除某些键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!