vendredi 31 juillet 2015

PHP function returning NULL instead of array

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.

Aucun commentaire:

Enregistrer un commentaire