youdontmeanmuch Posted January 12, 2009 Share Posted January 12, 2009 Per the example array at the very bottom, i want to be able to append the depth of each embedded array inside of the array. for example: array ( 53 => array ( 'title' => 'Home', 'path' => '', 'type' => '118', 'pid' => 52, 'hasChildren' => 0, ), Has a depth of one according to the sample array shown below so it should now look like this: array ( 53 => array ( 'title' => 'Home', 'path' => '', 'type' => '118', 'pid' => 52, 'hasChildren' => 0, 'depth' => 1, ), and so on... All of the recursive array function attempts i have made are pretty embarrassing. However I have looked at RecursiveArrayIterator which has the getDepth function. I'm confused on how to append it to the current array... any help is VERY much appreciated, thank you. <?php $example = array ( 52 => array ( 'title' => 'Website Navigation', 'path' => '', 'type' => '115', 'pid' => 0, 'hasChildren' => 1, 'children' => array ( 53 => array ( 'title' => 'Home', 'path' => '<front>', '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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/140490-solved-arrays-appending-depth-of-array-item-recursively-to-an-array/ Share on other sites More sharing options...
sasa Posted January 12, 2009 Share Posted January 12, 2009 try <?php $example = array ( 52 => array ( 'title' => 'Website Navigation', 'path' => '', 'type' => '115', 'pid' => 0, 'hasChildren' => 1, 'children' => array ( 53 => array ( 'title' => 'Home', 'path' => '<front>', '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, ), ), ), ), ), ); function add_depth($array, $depth=0){ foreach ($array as $k => $a){ if ($depth) $array[$k]['depth'] = $depth; if ($a['hasChildren']) $array[$k]['children'] = add_depth($a['children'], $depth+1); } return $array; } $example = add_depth($example); print_r($example); ?> Quote Link to comment https://forums.phpfreaks.com/topic/140490-solved-arrays-appending-depth-of-array-item-recursively-to-an-array/#findComment-735187 Share on other sites More sharing options...
youdontmeanmuch Posted January 12, 2009 Author Share Posted January 12, 2009 Thanks A LOT for your help... its missing the depth for items that have children. But i did find another solution here it is if anyone is curious: function array_set_depth($array, $depth = -1) { $subdepth = $depth + 1; if ($depth < 0) { foreach ($array as $key => $subarray) { $temp[$key] = array_set_depth(($subarray), $subdepth); } return $temp; } $array['depth'] = $depth; if ($array['hasChildren'] && isset($array['children'])) { foreach ($array['children'] as $key => $subarray) { $temp[$key] = array_set_depth($subarray, $subdepth); } unset($array['children']); $array['children'] = $temp; } return $array; } Quote Link to comment https://forums.phpfreaks.com/topic/140490-solved-arrays-appending-depth-of-array-item-recursively-to-an-array/#findComment-735196 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.