PHP arrays by adding the depth of an array element recursively to an array with the key 'depth'

In the example array at the very bottom, I want to be able to add the depth of each inline array inside the array. eg:

  array (
       53 => 
       array (
         'title' => 'Home',
         'path' => '',
         'type' => '118',
         'pid' => 52,
         'hasChildren' => 0,
       ),

It has depth in accordance with the array of samples below, so now it should look like this:

  array (
       53 => 
       array (
         'title' => 'Home',
         'path' => '',
         'type' => '118',
         'pid' => 52,
         'hasChildren' => 0,
         'depth' => 1,
       ),

etc.

All the attempts of the recursive array function that I have done are quite confusing. However, I looked at the RecursiveArrayIterator, which has a getDepth function. I am confused about how to add it to the current array ... any help is VERY much appreciated, thanks.

 
   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);

 ?>
+4
source share
5 answers

I assume that there is another array (not included in your sample code at the top. 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; } 

Usage example, I set your array to $ a:

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

Edit:

To set the depth in front of the children for a good print, 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; } 
+8
source

Should such a recursive function do this?

 function setDepth(&$a, $depth) { $a['depth']=$depth; foreach($a as $key=>$value) { if (is_array($value)) setDepth($a[$key], $depth+1); } } 

It should be noted that the array is passed by reference, so we can modify it. Note that we also use this link in the recursive call to setDepth. Although I used foreach for convenience, the $ value variable is a copy, and passing this value to setDepth will only make short changes within the foreach loop.

+5
source

Modified Pauls code to work with this example.

 function setDepth(&$a, $depth = -1) { if (($depth > -1) && !($depth % 2)) $a['depth']= $depth / 2; foreach($a as $key=>$value) { if (is_array($value)) setDepth($a[$key], $depth+1); } } setDepth($a); print_r($a); 
+4
source

sth how this should do the trick:

 function setdepth($arr, $depth = 0) { foreach ($arr as $key => $val) { $arr[$key]['depth'] = $depth; if ($arr[$key]['hasChildren']) { setdepth(&$arr[$key]['children'], $depth+1); } } } 

It would be easier for me if your array started with an index not with values, so an example of use might be like this:

 $arr[0] = $website; setdepth(&$arr, 0); 

where the website is an array from your example

0
source

This may be useful:

 function extend( $arr, $myArr=array() ) { foreach( $arr as $key => $value ) { if( is_array( $key ) ) { extend( $arr[ $key ] ); } else { $myArr[ $key ] = $arr[ $key ]; } } return $myArr; } 

A function called "expand" because it not only copies the array to a new one, but also extends the existing arrays.

To expand the array, you must put it as the second parameter, otherwise set an empty array. The lopps function through the properties of the array and checks whether it is an array or not, and if it is run again, otherwise it copies the values ​​to another array and returns it.

0
source

All Articles