0

Hi I'm have recursion function that makes binary tree array, so from that

array(
array(
   'name' => 'John',
   'id' => 1,
   'mother_id' => 2,
   'father_id' => 3
),
array(
   'name' => 'Lucy',
   'id' => 2,
   'mother_id' => 5,
   'father_id' => 4
),
array(
   'name' => 'Jim',
   'id' => 3,
   'mother_id' => 7,
   'father_id' => 9
),
array(
   'name' => 'Paul',
   'id' => 4,
   'mother_id' => 534,
   'father_id' => 54
),
array(
   'name' => 'Laura',
   'id' => 5,
   'mother_id' => 554,
   'father_id' => 51
),
array(
   'name' => 'Vanessa',
   'id' => 7,
   'mother_id' => 5354,
   'father_id' => 514
),
array(
   'name' => 'Adam',
   'id' => 9,
   'mother_id' => 245354,
   'father_id' => 514234
),
);

I'm getting that:

array(
 array(
'person' => array(
    'name' => 'John',
    'id' => 1,
    'mother_id' => 2,
    'father_id' => 3
),
'parents' => array(
    'mother' => array(
          'person' => array(
              'name' => 'Lucy',
              'id' => 2,
              'mother_id' => 5,
              'father_id' => 4
          ),
          'parents' => array(
              'mother' => array(
                  'person' => array(
                      'name' => 'Laura',
                      'id' => 5,
                      'mother_id' => 554,
                      'father_id' => 51
                  ),
                  'parents' => array(...)
              ),
              'father' => array(
                    'person' => array(
                        'name' => 'Paul',
                        'id' => 4,
                        'mother_id' => 534,
                        'father_id' => 54
                    ),
                    'parents' => array(...)
               ), 
          )
    ),
    'father' => ...
 )

and it works completely fine until it gets array with more than -+100 values, and when it gets this array, it returns 500 error, that's seems strange for me, cos obviously I don't have infinite loop or something like that, cos smallest arrays parses fine but I can't figure out what's reason for this kind of behavior.

function parseTree(&$tree, $root = null)
{
$return = null;
foreach ($tree as $key => $item) {
    if ($root == null || $item['id'] == $root) {
        $return = [
            'person' => $item,
            'parents' => [
                'father' => parseTree($tree, $item['father_id']),
                'mother' => parseTree($tree, $item['mother_id'])
            ]
        ];
        unset ($tree[$key]);
    }
}
return $return;
 }
Roman
  • 394
  • 1
  • 2
  • 12
  • 2
    Check this answer to a very similar question: http://stackoverflow.com/questions/4293775/increasing-nesting-functions-calls-limit – dlopez May 14 '16 at 16:08
  • Cool, I got answer on my question there, thanks a lot – Roman May 14 '16 at 16:39

0 Answers0