根据最底层的示例数组,我希望能够追加数组内部的每个嵌入数组的深度。例如:PHP数组,使用'深度'键递归地添加数组元素的深度
array ( 53 => array ( 'title' => 'Home', 'path' => '', 'type' => '118', 'pid' => 52, 'hasChildren' => 0, ),
已按如下所以现在应该是这样的样品阵列一个深度:
array ( 53 => array ( 'title' => 'Home', 'path' => '', 'type' => '118', 'pid' => 52, 'hasChildren' => 0, 'depth' => 1, ),
等等...
所有的我所做的递归数组函数尝试都很尴尬。不过,我已经看过具有getDepth函数的RecursiveArrayIterator。我很困惑如何将它附加到当前数组...任何帮助非常感谢,谢谢。
array ( 'title' => 'Website Navigation', 'path' => '', 'type' => '115', 'pid' => 0, 'hasChildren' => 1, 'children' => array ( 53 => array ( 'title' => 'Home', 'path' => '', 'type' => '118', 'pid' => 52, 'hasChildren' => 0, ), 54 => array ( 'title' => 'Features', 'path' => 'features', 'type' => '374', 'pid' => 52, 'hasChildren' => 1, 'children' => array ( 59 => array ( 'title' => 'artistic', 'path' => 'features/artistic', 'type' => '374', 'pid' => 54, 'hasChildren' => 1, 'children' => array ( 63 => array ( 'title' => 'galleries', 'path' => 'features/artistic/galleries', 'type' => '374', 'pid' => 59, 'hasChildren' => 1, 'children' => array ( 65 => array ( 'title' => 'graphics', 'path' => 'features/artistic/galleries/graphics', 'type' => '118', 'pid' => 63, 'hasChildren' => 0, ), 67 => array ( 'title' => 'mixed medium', 'path' => 'features/artistic/galleries/mixed-medium', 'type' => '118', 'pid' => 63, 'hasChildren' => 0, ), 64 => array ( 'title' => 'overview', 'path' => 'features/artistic/galleries', 'type' => '118', 'pid' => 63, 'hasChildren' => 0, ), 68 => array ( 'title' => 'photography', 'path' => 'features/artistic/galleries/photography', 'type' => '118', 'pid' => 63, 'hasChildren' => 0, ), 66 => array ( 'title' => 'traditional', 'path' => 'features/artistic/galleries/traditional', 'type' => '118', 'pid' => 63, 'hasChildren' => 0, ), ), ), 62 => array ( 'title' => 'overview', 'path' => 'features/artistic', 'type' => '118', 'pid' => 59, 'hasChildren' => 0, ), 69 => array ( 'title' => 'tutorials', 'path' => 'features/artistic/tutorials', 'type' => '374', 'pid' => 59, 'hasChildren' => 1, 'children' => array ( 71 => array ( 'title' => 'by category', 'path' => 'features/artistic/tutorials/by-category/', 'type' => '118', 'pid' => 69, 'hasChildren' => 0, ), 72 => array ( 'title' => 'by date', 'path' => 'features/artistic/tutorials/by-date/', 'type' => '118', 'pid' => 69, 'hasChildren' => 0, ), 70 => array ( 'title' => 'overview', 'path' => 'features/artistic/tutorials', 'type' => '118', 'pid' => 69, 'hasChildren' => 0, ), ), ), ), ), 58 => array ( 'title' => 'overview', 'path' => 'features', 'type' => '118', 'pid' => 54, 'hasChildren' => 0, ), 61 => array ( 'title' => 'projects/labs', 'path' => 'features/projects-labs/', 'type' => '374', 'pid' => 54, 'hasChildren' => 0, ), 60 => array ( 'title' => 'web development', 'path' => 'features/web-development', 'type' => '374', 'pid' => 54, 'hasChildren' => 1, 'children' => array ( 74 => array ( 'title' => 'articles', 'path' => 'features/web-development/articles/', 'type' => '374', 'pid' => 60, 'hasChildren' => 0, ), 73 => array ( 'title' => 'overview', 'path' => 'features/web-development', 'type' => '118', 'pid' => 60, 'hasChildren' => 0, ), 75 => array ( 'title' => 'tutorials', 'path' => 'features/web-development/tutorials', 'type' => '374', 'pid' => 60, 'hasChildren' => 0, ), ), ), ), ), 55 => array ( 'title' => 'Activity', 'path' => 'activity', 'type' => '374', 'pid' => 52, 'hasChildren' => 0, ), 56 => array ( 'title' => 'Blog', 'path' => 'blog', 'type' => '374', 'pid' => 52, 'hasChildren' => 0, ), 57 => array ( 'title' => 'About', 'path' => 'about', 'type' => '374', 'pid' => 52, 'hasChildren' => 1, 'children' => array ( 76 => array ( 'title' => 'the author', 'path' => 'about/the-author', 'type' => '118', 'pid' => 57, 'hasChildren' => 0, ), 77 => array ( 'title' => 'the website', 'path' => 'about/the-website', 'type' => '118', 'pid' => 57, 'hasChildren' => 0, ), ), ), ), ), ); print_r($example); ?>
这是一个数组的通用递归函数。这个例子需要一些工作。 – OIS 2009-01-12 13:12:38
我修改了你的代码,就像它应该那样工作。不知道如何发布。 – OIS 2009-01-12 13:19:00