0

Not really much to ask. I want print array but without using recursion. The reason is that program has a bug because it's too much data and old function has to be rewritten. The function is called recursive and after many calls my script can't allocate more memory, so my example array is:

array (
  0 => 
  array (
    'id' => '5713008',
    'children' => 
    array (
      0 => 
      array (
        0 => 
        array (
          'id' => '13',
          'children' => 
          array (
            'id' => '20',
            'children' => 
            array (
              0 => 
              array (
                'id' => '120',
              ),
              1 => 
              array (
                'id' => '464',
              ),
            ),
          ),
        ),
        1 => 
        array (
          0 => 
          array (
            'id' => '21',
          ),
        ),
      ),
      1 => 
      array (
        'id' => '143',
        'children' => 
        array (
          1 => 
          array (
            'id' => '60011',
          ),
          2 => 
          array (
            'id' => '60012',
          ),
          3 => 
          array (
            'id' => '5299316',
          ),
          4 => 
          array (
            'id' => '5712901',
          ),
          5 => 
          array (
            'id' => '147',
          ),
          6 => 
          array (
            'id' => '148',
          ),
          7 => 
          array (
            'id' => '149',
          ),
          8 => 
          array (
            'id' => '60010',
          ),
        ),
      ),
    ),
  ),
)

I need data something like this:

id: 5713008
--------------
>>id: 13
--------------
>>>>id: 20
--------------
>>>>>>>>id: 120
--------------
>>>>>>>>id: 464
--------------
>>>>id: 21
--------------
>>id: 143
--------------
>>>>id: 60011
--------------
>>>>id: 60012
--------------
>>>>id: 5299316
--------------
>>>>id: 5712901
--------------
>>>>id: 147
--------------
>>>>id: 148
--------------
>>>>id: 149
--------------
>>>>id: 60010
--------------
tstr
  • 1,254
  • 20
  • 35
  • possible duplicate of [Transform flat array to tree with one-time loop](http://stackoverflow.com/questions/19927176/transform-flat-array-to-tree-with-one-time-loop) – Alma Do Jul 21 '14 at 14:33

1 Answers1

1
function myVarDump($item, $recursion = 0)
{
  if(is_array($item))
    foreach($item as $key => $value)
    {
      if(is_array($value))
        myVarDump($value, $recursion+1);     
      else
        echo str_repeat('>', $recursion)."$key: $value\n--------------\n";
    }
  else echo($item);
}

This function will output the data as you request, however obviously you'll still need recursion for this to show the nested items. Therefore I don't exactly understand the problem you're trying to solve - perhaps you need to limit recursion? If so just add a limit to how high $recursion can become in this function, for example like this:

function myVarDump($item, $recursion = 0, $maxRecursion = 5)
{
  if($recursion > $maxRecursion)
    echo str_repeat('>>', $recursion)."ERROR: Max recursion $maxRecursion exceeded";
  elseif(is_array($item))
    foreach($item as $key => $value)
    {
      if(is_array($value))
        myVarDump($value, $recursion+1);     
      else
        echo str_repeat('>', $recursion)."$key: $value\n--------------\n";
    }
  else echo($item);
}
Niels Keurentjes
  • 41,402
  • 9
  • 98
  • 136
  • Edited that in since I suspect that's his problem indeed. – Niels Keurentjes Jul 21 '14 at 14:30
  • Well, not sure how this corresponds to demanded "without using recursion" – Alma Do Jul 21 '14 at 14:32
  • @AlmaDo his required output is impossible without using recursion. Even flattening the array would require it. – Niels Keurentjes Jul 21 '14 at 14:33
  • Ok, my remark was incomplete, admitted. But the point remains - if the recursion blows up his stack, a stack data structure logically would too - they contain the same number of iterations, and essentially subroutine calls are a flat stack data structure with loops at the assembly level. Therefore I'm extremely curious what the real [XY problem](http://meta.stackexchange.com/a/66378) is here. – Niels Keurentjes Jul 21 '14 at 14:38
  • Well, I retracted the vote (since task is reverse, not flatten to tree, but tree to flatten) - but the issue is same, it's possible to implement any recursion with stack+loops. However, point about XY is correct, but my point is - to provide then both ways in the answer (so with recursion and without) and then OP will be able to figure out where he's making wrong decision junction. It's not applicable always, but in this case my point was about to show both ways. So don't worry - your answer isn't _incorrect_, it _may be_ just incomplete (that's only my point, though.. ) – Alma Do Jul 21 '14 at 14:41
  • Well agreed then on all points made, but before I write another load of code like this I'm going to wait for the X in the Y ;) – Niels Keurentjes Jul 21 '14 at 14:43