3

I call a function that does some recursion and is supposed to return an array. In fact, a var_dump immediately before the return statement in the called function evinces the array; however, a var_dump of the results from the calling function reveals NULL instead of the array.

Here's the calling function.

<?php  

// configuration
require_once("../includes/config.php");
require_once("../includes/getParentNodes.php");  

$bottomNode = 17389;
$chain = [];
$chain[] = $bottomNode;
$results = getParentNodes($bottomNode,$chain);

var_dump($results); ?>

Here's the called function.

<?php

function getParentNodes($node, $results)
{
    $select = query("SELECT parent_id FROM classifications WHERE node_id = ?", $node);
    $parent = implode("",$select[0]);
    if (!empty($parent))
    {
        $results[] = $parent;
        getParentNodes($parent,$results);   
    }
    else
    {
        return $results;
    }
}
?>

If I place a var_dump immediately preceding the return call, I get the following.

Array
(
    [0] => 17389
    [1] => 17386
    [2] => 17334
    [3] => 16788
    [4] => 15157
    [5] => 10648
    [6] => 3962
    [7] => 665
    [8] => 39
    [9] => 1
)

However, the var_dump in the calling function produces a NULL.

I've read the manual and the related posts, but none shed light on this problem. Any help would be much appreciated.

Martin
  • 22,212
  • 11
  • 70
  • 132
John
  • 149
  • 3
  • 14
  • What version of PHP are you using? > 5.3 I hope, which is probably your problem. The reason is that short array syntax wasn't a thing until PHP 5.4 – ArtisticPhoenix Aug 01 '15 at 02:05
  • change your value of `function getParentNodes($node, $results)` to rename this `$results` variable to another name, so stop name overlaps. – Martin Aug 01 '15 at 02:10
  • When you make the recursive called to `getParentNodes()` from within the function you discard the returned value so nothing gets passed back up the chain. –  Aug 01 '15 at 02:10
  • @ArtisitcPhoenix, i'm running version 5.6.8. – John Aug 01 '15 at 09:24
  • @Martin, good observation. I actually changed those variables subsequent to the post to no effect. – John Aug 01 '15 at 09:25

2 Answers2

4

You're missing a return in the recursive case.

      $results[] = $parent;
      return getParentNodes($parent,$results);   
Mark Reed
  • 91,912
  • 16
  • 138
  • 175
  • wow do I feel silly. You are correct. I totally missed the other return call. It works perfectly now. Many thanks Mark – John Aug 01 '15 at 09:29
  • Glad I could help. If you could please accept the answer, that'd be great. :) – Mark Reed Aug 01 '15 at 13:25
2

There is a little error on the code:

function getParentNodes($node, $results)
{
    $select = query("SELECT parent_id FROM classifications WHERE node_id = ?", $node);
    $parent = implode("",$select[0]);
    if (!empty($parent))
    {
        $results[] = $parent;
        return getParentNodes($parent,$results); // <- modified
    }
    else
    {
        return $results;
    }
}
fbiazi
  • 387
  • 2
  • 10