我正在尝试使用父子关系构建分层关联数组。输入数据以元素数组的形式出现。每个元素都有一个id和一个父id。
为了提高效率,当一个元素找到它的父元素时,我想从源数据中删除那个子元素,这样就不会浪费周期来检查该元素是否是其他元素的子元素。
以下是代码:
// parentid == 0 indicates a root node
function buildTree(&$elements, $parentid=0){
$branch = array();
foreach($elements as $key => $element){
if($element['parentid'] == $parentid){
$twig = $element;
unset($elements[$key]);
$twig['children'] = buildTree($elements, $element['pageid']);
$branch[] = $twig;
}
}
return $branch;
}
我遇到的问题是,在某些情况下,某些元素永远不会找到他们的父母。
我认为这种情况正在发生,因为$elements
变量正在通过引用传递,更深层次的递归破坏了外部级别的内部数组指针的位置,导致其他子项被跳过而未被触及在外层的foreach循环中。
我试图看看是否有一种方法可以在进入另一个递归级别之前保存内部数组指针的位置,然后在函数返回时恢复位置,但它不会出现这在PHP中是可行的。
如果我将&
取出来不通过引用传递$elements
变量,那么该函数可以正常工作。不幸的是,即使这些元素已被保存在其正确的父级之下,也会留下由foreach处理的元素,浪费了一些处理时间。
有关如何将未分类的元素池中的子元素分配给其父元素之后如何删除子元素的任何想法?
编辑:当然,一些示例数据会有所帮助:
$source_data = unserialize('a:33:{i:0;a:5:{s:6:"pageid";s:2:"53";s:3:"key";s:2:"53";s:8:"parentid";s:2:"52";s:7:"tooltip";s:0:"";s:5:"title";s:8:"Example 2.0";}i:1;a:5:{s:6:"pageid";s:2:"68";s:3:"key";s:2:"68";s:8:"parentid";s:2:"53";s:7:"tooltip";s:0:"";s:5:"title";s:5:"Audit";}i:2;a:5:{s:6:"pageid";s:2:"65";s:3:"key";s:2:"65";s:8:"parentid";s:2:"25";s:7:"tooltip";s:26:"CastIron Deployment Helper";s:5:"title";s:8:"helper";}i:3;a:5:{s:6:"pageid";s:2:"50";s:3:"key";s:2:"50";s:8:"parentid";s:2:"49";s:7:"tooltip";s:17:"Calendar Template";s:5:"title";s:8:"Calendar";}i:4;a:5:{s:6:"pageid";s:2:"28";s:3:"key";s:2:"28";s:8:"parentid";s:2:"27";s:7:"tooltip";s:0:"";s:5:"title";s:15:"querty Gifts";}i:5;a:5:{s:6:"pageid";s:2:"24";s:3:"key";s:2:"24";s:8:"parentid";s:1:"0";s:7:"tooltip";s:0:"";s:5:"title";s:4:"More_Samples";}i:6;a:5:{s:6:"pageid";s:2:"35";s:3:"key";s:2:"35";s:8:"parentid";s:1:"0";s:7:"tooltip";s:38:"Ideas for features of this wiki webapp";s:5:"title";s:6:"Design";}i:7;a:5:{s:6:"pageid";s:2:"70";s:3:"key";s:2:"70";s:8:"parentid";s:2:"25";s:7:"tooltip";s:0:"";s:5:"title";s:26:"Environments & Credentials";}i:8;a:5:{s:6:"pageid";s:2:"67";s:3:"key";s:2:"67";s:8:"parentid";s:2:"36";s:7:"tooltip";s:0:"";s:5:"title";s:19:"Helping Downstreams";}i:9;a:5:{s:6:"pageid";s:1:"1";s:3:"key";s:1:"1";s:8:"parentid";s:1:"0";s:7:"tooltip";s:0:"";s:5:"title";s:4:"Home";}i:10;a:5:{s:6:"pageid";s:2:"64";s:3:"key";s:2:"64";s:8:"parentid";s:2:"53";s:7:"tooltip";s:0:"";s:5:"title";s:5:"ITC02";}i:11;a:5:{s:6:"pageid";s:2:"27";s:3:"key";s:2:"27";s:8:"parentid";s:1:"0";s:7:"tooltip";s:0:"";s:5:"title";s:14:"More_Samples topics";}i:12;a:5:{s:6:"pageid";s:2:"26";s:3:"key";s:2:"26";s:8:"parentid";s:2:"25";s:7:"tooltip";s:0:"";s:5:"title";s:2:"MQ";}i:13;a:5:{s:6:"pageid";s:2:"55";s:3:"key";s:2:"55";s:8:"parentid";s:2:"36";s:7:"tooltip";s:0:"";s:5:"title";s:5:"Music";}i:14;a:5:{s:6:"pageid";s:2:"73";s:3:"key";s:2:"73";s:8:"parentid";s:2:"52";s:7:"tooltip";s:0:"";s:5:"title";s:5:"Other";}i:15;a:5:{s:6:"pageid";s:2:"29";s:3:"key";s:2:"29";s:8:"parentid";s:1:"1";s:7:"tooltip";s:0:"";s:5:"title";s:3:"Sample";}i:16;a:5:{s:6:"pageid";s:2:"34";s:3:"key";s:2:"34";s:8:"parentid";s:2:"29";s:7:"tooltip";s:0:"";s:5:"title";s:14:"Sample 2013 Goals";}i:17;a:5:{s:6:"pageid";s:2:"33";s:3:"key";s:2:"33";s:8:"parentid";s:2:"29";s:7:"tooltip";s:0:"";s:5:"title";s:8:"Sample 2014";}i:18;a:5:{s:6:"pageid";s:2:"31";s:3:"key";s:2:"31";s:8:"parentid";s:2:"29";s:7:"tooltip";s:0:"";s:5:"title";s:6:"Sample R2";}i:19;a:5:{s:6:"pageid";s:2:"32";s:3:"key";s:2:"32";s:8:"parentid";s:2:"29";s:7:"tooltip";s:0:"";s:5:"title";s:13:"Sample Text Only";}i:20;a:5:{s:6:"pageid";s:2:"25";s:3:"key";s:2:"25";s:8:"parentid";s:1:"0";s:7:"tooltip";s:0:"";s:5:"title";s:9:"Reference";}i:21;a:5:{s:6:"pageid";s:2:"62";s:3:"key";s:2:"62";s:8:"parentid";s:2:"52";s:7:"tooltip";s:0:"";s:5:"title";s:8:"S&D reps";}i:22;a:5:{s:6:"pageid";s:2:"36";s:3:"key";s:2:"36";s:8:"parentid";s:1:"0";s:7:"tooltip";s:0:"";s:5:"title";s:7:"Sandbox";}i:23;a:5:{s:6:"pageid";s:2:"71";s:3:"key";s:2:"71";s:8:"parentid";s:2:"52";s:7:"tooltip";s:0:"";s:5:"title";s:9:"Schedules";}i:24;a:5:{s:6:"pageid";s:2:"72";s:3:"key";s:2:"72";s:8:"parentid";s:2:"52";s:7:"tooltip";s:0:"";s:5:"title";s:9:"Schedules";}i:25;a:5:{s:6:"pageid";s:2:"51";s:3:"key";s:2:"51";s:8:"parentid";s:1:"0";s:7:"tooltip";s:0:"";s:5:"title";s:13:"Special Pages";}i:26;a:5:{s:6:"pageid";s:2:"63";s:3:"key";s:2:"63";s:8:"parentid";s:2:"35";s:7:"tooltip";s:36:"How to handle tasks and task parsing";s:5:"title";s:5:"Tasks";}i:27;a:5:{s:6:"pageid";s:2:"49";s:3:"key";s:2:"49";s:8:"parentid";s:1:"0";s:7:"tooltip";s:47:"Template pages for use when creating new pages.";s:5:"title";s:9:"Templates";}i:28;a:5:{s:6:"pageid";s:2:"52";s:3:"key";s:2:"52";s:8:"parentid";s:1:"0";s:7:"tooltip";s:0:"";s:5:"title";s:4:"Work";}i:29;a:5:{s:6:"pageid";s:2:"14";s:3:"key";s:2:"14";s:8:"parentid";s:1:"1";s:7:"tooltip";s:0:"";s:5:"title";s:8:"another!";}i:30;a:5:{s:6:"pageid";s:2:"54";s:3:"key";s:2:"54";s:8:"parentid";s:2:"25";s:7:"tooltip";s:0:"";s:5:"title";s:4:"tmux";}i:31;a:5:{s:6:"pageid";s:2:"12";s:3:"key";s:2:"12";s:8:"parentid";s:1:"1";s:7:"tooltip";s:0:"";s:5:"title";s:5:"what?";}i:32;a:5:{s:6:"pageid";s:2:"13";s:3:"key";s:2:"13";s:8:"parentid";s:1:"1";s:7:"tooltip";s:0:"";s:5:"title";s:5:"zippy";}}');
$correct_result = unserialize('a:9:{i:0;a:7:{s:6:"pageid";s:2:"24";s:3:"key";s:2:"24";s:8:"parentid";s:1:"0";s:7:"tooltip";s:0:"";s:5:"title";s:4:"More_Samples";s:4:"href";s:8:"?page=24";s:8:"children";a:0:{}}i:1;a:7:{s:6:"pageid";s:2:"35";s:3:"key";s:2:"35";s:8:"parentid";s:1:"0";s:7:"tooltip";s:38:"Ideas for features of this wiki webapp";s:5:"title";s:6:"Design";s:4:"href";s:8:"?page=35";s:8:"children";a:1:{i:0;a:7:{s:6:"pageid";s:2:"63";s:3:"key";s:2:"63";s:8:"parentid";s:2:"35";s:7:"tooltip";s:36:"How to handle tasks and task parsing";s:5:"title";s:5:"Tasks";s:4:"href";s:8:"?page=63";s:8:"children";a:0:{}}}}i:2;a:7:{s:6:"pageid";s:1:"1";s:3:"key";s:1:"1";s:8:"parentid";s:1:"0";s:7:"tooltip";s:0:"";s:5:"title";s:4:"Home";s:4:"href";s:7:"?page=1";s:8:"children";a:4:{i:0;a:7:{s:6:"pageid";s:2:"29";s:3:"key";s:2:"29";s:8:"parentid";s:1:"1";s:7:"tooltip";s:0:"";s:5:"title";s:3:"Sample";s:4:"href";s:8:"?page=29";s:8:"children";a:4:{i:0;a:7:{s:6:"pageid";s:2:"34";s:3:"key";s:2:"34";s:8:"parentid";s:2:"29";s:7:"tooltip";s:0:"";s:5:"title";s:14:"Sample 2013 Goals";s:4:"href";s:8:"?page=34";s:8:"children";a:0:{}}i:1;a:7:{s:6:"pageid";s:2:"33";s:3:"key";s:2:"33";s:8:"parentid";s:2:"29";s:7:"tooltip";s:0:"";s:5:"title";s:8:"Sample 2014";s:4:"href";s:8:"?page=33";s:8:"children";a:0:{}}i:2;a:7:{s:6:"pageid";s:2:"31";s:3:"key";s:2:"31";s:8:"parentid";s:2:"29";s:7:"tooltip";s:0:"";s:5:"title";s:6:"Sample R2";s:4:"href";s:8:"?page=31";s:8:"children";a:0:{}}i:3;a:7:{s:6:"pageid";s:2:"32";s:3:"key";s:2:"32";s:8:"parentid";s:2:"29";s:7:"tooltip";s:0:"";s:5:"title";s:13:"Sample Text Only";s:4:"href";s:8:"?page=32";s:8:"children";a:0:{}}}}i:1;a:7:{s:6:"pageid";s:2:"14";s:3:"key";s:2:"14";s:8:"parentid";s:1:"1";s:7:"tooltip";s:0:"";s:5:"title";s:8:"another!";s:4:"href";s:8:"?page=14";s:8:"children";a:0:{}}i:2;a:7:{s:6:"pageid";s:2:"12";s:3:"key";s:2:"12";s:8:"parentid";s:1:"1";s:7:"tooltip";s:0:"";s:5:"title";s:5:"what?";s:4:"href";s:8:"?page=12";s:8:"children";a:0:{}}i:3;a:7:{s:6:"pageid";s:2:"13";s:3:"key";s:2:"13";s:8:"parentid";s:1:"1";s:7:"tooltip";s:0:"";s:5:"title";s:5:"zippy";s:4:"href";s:8:"?page=13";s:8:"children";a:0:{}}}}i:3;a:7:{s:6:"pageid";s:2:"27";s:3:"key";s:2:"27";s:8:"parentid";s:1:"0";s:7:"tooltip";s:0:"";s:5:"title";s:14:"More_Samples topics";s:4:"href";s:8:"?page=27";s:8:"children";a:1:{i:0;a:7:{s:6:"pageid";s:2:"28";s:3:"key";s:2:"28";s:8:"parentid";s:2:"27";s:7:"tooltip";s:0:"";s:5:"title";s:15:"querty Gifts";s:4:"href";s:8:"?page=28";s:8:"children";a:0:{}}}}i:4;a:7:{s:6:"pageid";s:2:"25";s:3:"key";s:2:"25";s:8:"parentid";s:1:"0";s:7:"tooltip";s:0:"";s:5:"title";s:9:"Reference";s:4:"href";s:8:"?page=25";s:8:"children";a:4:{i:0;a:7:{s:6:"pageid";s:2:"65";s:3:"key";s:2:"65";s:8:"parentid";s:2:"25";s:7:"tooltip";s:26:"CastIron Deployment Helper";s:5:"title";s:8:"helper";s:4:"href";s:8:"?page=65";s:8:"children";a:0:{}}i:1;a:7:{s:6:"pageid";s:2:"70";s:3:"key";s:2:"70";s:8:"parentid";s:2:"25";s:7:"tooltip";s:0:"";s:5:"title";s:26:"Environments & Credentials";s:4:"href";s:8:"?page=70";s:8:"children";a:0:{}}i:2;a:7:{s:6:"pageid";s:2:"26";s:3:"key";s:2:"26";s:8:"parentid";s:2:"25";s:7:"tooltip";s:0:"";s:5:"title";s:2:"MQ";s:4:"href";s:8:"?page=26";s:8:"children";a:0:{}}i:3;a:7:{s:6:"pageid";s:2:"54";s:3:"key";s:2:"54";s:8:"parentid";s:2:"25";s:7:"tooltip";s:0:"";s:5:"title";s:4:"tmux";s:4:"href";s:8:"?page=54";s:8:"children";a:0:{}}}}i:5;a:7:{s:6:"pageid";s:2:"36";s:3:"key";s:2:"36";s:8:"parentid";s:1:"0";s:7:"tooltip";s:0:"";s:5:"title";s:7:"Sandbox";s:4:"href";s:8:"?page=36";s:8:"children";a:2:{i:0;a:7:{s:6:"pageid";s:2:"67";s:3:"key";s:2:"67";s:8:"parentid";s:2:"36";s:7:"tooltip";s:0:"";s:5:"title";s:19:"Helping Downstreams";s:4:"href";s:8:"?page=67";s:8:"children";a:0:{}}i:1;a:7:{s:6:"pageid";s:2:"55";s:3:"key";s:2:"55";s:8:"parentid";s:2:"36";s:7:"tooltip";s:0:"";s:5:"title";s:5:"Music";s:4:"href";s:8:"?page=55";s:8:"children";a:0:{}}}}i:6;a:7:{s:6:"pageid";s:2:"51";s:3:"key";s:2:"51";s:8:"parentid";s:1:"0";s:7:"tooltip";s:0:"";s:5:"title";s:13:"Special Pages";s:4:"href";s:8:"?page=51";s:8:"children";a:0:{}}i:7;a:7:{s:6:"pageid";s:2:"49";s:3:"key";s:2:"49";s:8:"parentid";s:1:"0";s:7:"tooltip";s:47:"Template pages for use when creating new pages.";s:5:"title";s:9:"Templates";s:4:"href";s:8:"?page=49";s:8:"children";a:1:{i:0;a:7:{s:6:"pageid";s:2:"50";s:3:"key";s:2:"50";s:8:"parentid";s:2:"49";s:7:"tooltip";s:17:"Calendar Template";s:5:"title";s:8:"Calendar";s:4:"href";s:8:"?page=50";s:8:"children";a:0:{}}}}i:8;a:7:{s:6:"pageid";s:2:"52";s:3:"key";s:2:"52";s:8:"parentid";s:1:"0";s:7:"tooltip";s:0:"";s:5:"title";s:4:"Work";s:4:"href";s:8:"?page=52";s:8:"children";a:5:{i:0;a:7:{s:6:"pageid";s:2:"53";s:3:"key";s:2:"53";s:8:"parentid";s:2:"52";s:7:"tooltip";s:0:"";s:5:"title";s:8:"Example 2.0";s:4:"href";s:8:"?page=53";s:8:"children";a:2:{i:0;a:7:{s:6:"pageid";s:2:"68";s:3:"key";s:2:"68";s:8:"parentid";s:2:"53";s:7:"tooltip";s:0:"";s:5:"title";s:5:"Audit";s:4:"href";s:8:"?page=68";s:8:"children";a:0:{}}i:1;a:7:{s:6:"pageid";s:2:"64";s:3:"key";s:2:"64";s:8:"parentid";s:2:"53";s:7:"tooltip";s:0:"";s:5:"title";s:5:"ITC02";s:4:"href";s:8:"?page=64";s:8:"children";a:0:{}}}}i:1;a:7:{s:6:"pageid";s:2:"73";s:3:"key";s:2:"73";s:8:"parentid";s:2:"52";s:7:"tooltip";s:0:"";s:5:"title";s:5:"Other";s:4:"href";s:8:"?page=73";s:8:"children";a:0:{}}i:2;a:7:{s:6:"pageid";s:2:"62";s:3:"key";s:2:"62";s:8:"parentid";s:2:"52";s:7:"tooltip";s:0:"";s:5:"title";s:8:"S&D reps";s:4:"href";s:8:"?page=62";s:8:"children";a:0:{}}i:3;a:7:{s:6:"pageid";s:2:"71";s:3:"key";s:2:"71";s:8:"parentid";s:2:"52";s:7:"tooltip";s:0:"";s:5:"title";s:9:"Schedules";s:4:"href";s:8:"?page=71";s:8:"children";a:0:{}}i:4;a:7:{s:6:"pageid";s:2:"72";s:3:"key";s:2:"72";s:8:"parentid";s:2:"52";s:7:"tooltip";s:0:"";s:5:"title";s:9:"Schedules";s:4:"href";s:8:"?page=72";s:8:"children";a:0:{}}}}}';
$by_reference = unserialize('a:9:{i:0;a:7:{s:6:"pageid";s:2:"24";s:3:"key";s:2:"24";s:8:"parentid";s:1:"0";s:7:"tooltip";s:0:"";s:5:"title";s:4:"More_Samples";s:4:"href";s:8:"?page=24";s:8:"children";a:0:{}}i:1;a:7:{s:6:"pageid";s:2:"35";s:3:"key";s:2:"35";s:8:"parentid";s:1:"0";s:7:"tooltip";s:38:"Ideas for features of this wiki webapp";s:5:"title";s:6:"Design";s:4:"href";s:8:"?page=35";s:8:"children";a:1:{i:0;a:7:{s:6:"pageid";s:2:"63";s:3:"key";s:2:"63";s:8:"parentid";s:2:"35";s:7:"tooltip";s:36:"How to handle tasks and task parsing";s:5:"title";s:5:"Tasks";s:4:"href";s:8:"?page=63";s:8:"children";a:0:{}}}}i:2;a:7:{s:6:"pageid";s:1:"1";s:3:"key";s:1:"1";s:8:"parentid";s:1:"0";s:7:"tooltip";s:0:"";s:5:"title";s:4:"Home";s:4:"href";s:7:"?page=1";s:8:"children";a:1:{i:0;a:7:{s:6:"pageid";s:2:"29";s:3:"key";s:2:"29";s:8:"parentid";s:1:"1";s:7:"tooltip";s:0:"";s:5:"title";s:3:"Sample";s:4:"href";s:8:"?page=29";s:8:"children";a:4:{i:0;a:7:{s:6:"pageid";s:2:"34";s:3:"key";s:2:"34";s:8:"parentid";s:2:"29";s:7:"tooltip";s:0:"";s:5:"title";s:14:"Sample 2013 Goals";s:4:"href";s:8:"?page=34";s:8:"children";a:0:{}}i:1;a:7:{s:6:"pageid";s:2:"33";s:3:"key";s:2:"33";s:8:"parentid";s:2:"29";s:7:"tooltip";s:0:"";s:5:"title";s:8:"Sample 2014";s:4:"href";s:8:"?page=33";s:8:"children";a:0:{}}i:2;a:7:{s:6:"pageid";s:2:"31";s:3:"key";s:2:"31";s:8:"parentid";s:2:"29";s:7:"tooltip";s:0:"";s:5:"title";s:6:"Sample R2";s:4:"href";s:8:"?page=31";s:8:"children";a:0:{}}i:3;a:7:{s:6:"pageid";s:2:"32";s:3:"key";s:2:"32";s:8:"parentid";s:2:"29";s:7:"tooltip";s:0:"";s:5:"title";s:13:"Sample Text Only";s:4:"href";s:8:"?page=32";s:8:"children";a:0:{}}}}}}i:3;a:7:{s:6:"pageid";s:2:"27";s:3:"key";s:2:"27";s:8:"parentid";s:1:"0";s:7:"tooltip";s:0:"";s:5:"title";s:14:"More_Samples topics";s:4:"href";s:8:"?page=27";s:8:"children";a:1:{i:0;a:7:{s:6:"pageid";s:2:"28";s:3:"key";s:2:"28";s:8:"parentid";s:2:"27";s:7:"tooltip";s:0:"";s:5:"title";s:15:"querty Gifts";s:4:"href";s:8:"?page=28";s:8:"children";a:0:{}}}}i:4;a:7:{s:6:"pageid";s:2:"25";s:3:"key";s:2:"25";s:8:"parentid";s:1:"0";s:7:"tooltip";s:0:"";s:5:"title";s:9:"Reference";s:4:"href";s:8:"?page=25";s:8:"children";a:4:{i:0;a:7:{s:6:"pageid";s:2:"65";s:3:"key";s:2:"65";s:8:"parentid";s:2:"25";s:7:"tooltip";s:26:"CastIron Deployment Helper";s:5:"title";s:8:"helper";s:4:"href";s:8:"?page=65";s:8:"children";a:0:{}}i:1;a:7:{s:6:"pageid";s:2:"70";s:3:"key";s:2:"70";s:8:"parentid";s:2:"25";s:7:"tooltip";s:0:"";s:5:"title";s:26:"Environments & Credentials";s:4:"href";s:8:"?page=70";s:8:"children";a:0:{}}i:2;a:7:{s:6:"pageid";s:2:"26";s:3:"key";s:2:"26";s:8:"parentid";s:2:"25";s:7:"tooltip";s:0:"";s:5:"title";s:2:"MQ";s:4:"href";s:8:"?page=26";s:8:"children";a:0:{}}i:3;a:7:{s:6:"pageid";s:2:"54";s:3:"key";s:2:"54";s:8:"parentid";s:2:"25";s:7:"tooltip";s:0:"";s:5:"title";s:4:"tmux";s:4:"href";s:8:"?page=54";s:8:"children";a:0:{}}}}i:5;a:7:{s:6:"pageid";s:2:"36";s:3:"key";s:2:"36";s:8:"parentid";s:1:"0";s:7:"tooltip";s:0:"";s:5:"title";s:7:"Sandbox";s:4:"href";s:8:"?page=36";s:8:"children";a:2:{i:0;a:7:{s:6:"pageid";s:2:"67";s:3:"key";s:2:"67";s:8:"parentid";s:2:"36";s:7:"tooltip";s:0:"";s:5:"title";s:19:"Helping Downstreams";s:4:"href";s:8:"?page=67";s:8:"children";a:0:{}}i:1;a:7:{s:6:"pageid";s:2:"55";s:3:"key";s:2:"55";s:8:"parentid";s:2:"36";s:7:"tooltip";s:0:"";s:5:"title";s:5:"Music";s:4:"href";s:8:"?page=55";s:8:"children";a:0:{}}}}i:6;a:7:{s:6:"pageid";s:2:"51";s:3:"key";s:2:"51";s:8:"parentid";s:1:"0";s:7:"tooltip";s:0:"";s:5:"title";s:13:"Special Pages";s:4:"href";s:8:"?page=51";s:8:"children";a:0:{}}i:7;a:7:{s:6:"pageid";s:2:"49";s:3:"key";s:2:"49";s:8:"parentid";s:1:"0";s:7:"tooltip";s:47:"Template pages for use when creating new pages.";s:5:"title";s:9:"Templates";s:4:"href";s:8:"?page=49";s:8:"children";a:1:{i:0;a:7:{s:6:"pageid";s:2:"50";s:3:"key";s:2:"50";s:8:"parentid";s:2:"49";s:7:"tooltip";s:17:"Calendar Template";s:5:"title";s:8:"Calendar";s:4:"href";s:8:"?page=50";s:8:"children";a:0:{}}}}i:8;a:7:{s:6:"pageid";s:2:"52";s:3:"key";s:2:"52";s:8:"parentid";s:1:"0";s:7:"tooltip";s:0:"";s:5:"title";s:4:"Work";s:4:"href";s:8:"?page=52";s:8:"children";a:1:{i:0;a:7:{s:6:"pageid";s:2:"53";s:3:"key";s:2:"53";s:8:"parentid";s:2:"52";s:7:"tooltip";s:0:"";s:5:"title";s:8:"Example 2.0";s:4:"href";s:8:"?page=53";s:8:"children";a:2:{i:0;a:7:{s:6:"pageid";s:2:"68";s:3:"key";s:2:"68";s:8:"parentid";s:2:"53";s:7:"tooltip";s:0:"";s:5:"title";s:5:"Audit";s:4:"href";s:8:"?page=68";s:8:"children";a:0:{}}i:1;a:7:{s:6:"pageid";s:2:"64";s:3:"key";s:2:"64";s:8:"parentid";s:2:"53";s:7:"tooltip";s:0:"";s:5:"title";s:5:"ITC02";s:4:"href";s:8:"?page=64";s:8:"children";a:0:{}}}}}}}';
$source_data
是未分类的源数据,$correct_result
是我通过引用传递$elements
时得到的结果(这是我想要的),{{1}我通过引用传递$by_reference
时得到的结果(错误的结果)。
答案 0 :(得分:0)
你必须在unset之后重新索引$ elements数组,试试这个..
unset($elements[$key]);
$temp_array=array_values($elements);
$twig['children'] = buildTree($temp_array, $element['id']);