Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 110977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:18:43+00:00 2026-05-11T02:18:43+00:00

Per the example array at the very bottom, i want to be able to

  • 0

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.

     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);  ?> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T02:18:44+00:00Added an answer on May 11, 2026 at 2:18 am

    I assume there is another array( at the top not included in your example code. Something like this?

    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);     }   }   if ($array['hasChildren'] && isset($array['children'])) {     foreach ($array['children'] as $key => $subarray) {       $temp[$key] = array_set_depth($subarray, $subdepth);     }     $array['children'] = $temp;   }   $array['depth'] = $depth;   return $array; } 

    Example usage, I set your array to the value $a:

    $b = array_set_depth($a); print_r($b); 

    Edit:

    To set depth before the children for nice printing you can do this:

    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; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 64k
  • Answers 64k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I wonder if it isn't a different 'this' at that… May 11, 2026 at 10:53 am
  • added an answer This is discussed in 'Effective C++' by Scott Meyer. The… May 11, 2026 at 10:53 am
  • added an answer I got a lot of help from #dbix-class. There may… May 11, 2026 at 10:53 am

Related Questions

Per the example array at the very bottom, i want to be able to
Per the Java documentation, the hash code for a String object is computed as:
As per the Emacs docs , every time you open a file, Emacs changes
as per the title; is it possible to have nested comments in valid HTML?
Having integrated merb_auth_password_slice as per the README, I can successfully login as redirect_after_login is
The RoR tutorials posit one model per table for the ORM to work. My
How do you return 1 value per row of the max of several columns:
Most solutions I've read here for supporting subdomain-per-user at the DNS level are to
In wxWidgets, how can you find the pixels per inch on a wxDC? I'd
What is the best way to do per-user database connections in Rails ? I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.