存储数组中的递归函数值(php)

时间:2016-08-01 13:17:25

标签: php arrays

我有函数找到我已创建函数的所有子元素我获取父ID但不能保存在一个数组中并返回一个数组而不是最后一个值插入。我这里的array_push不起作用。

function has_parent($parent,$con) {
    $return  = array(); 
        $selectparent = "select * from table where id = $parent ";
        $qResult = mysqli_query($conobj,$selectparent);
        $qRow = mysqli_fetch_assoc($qResult);
        $parent_id = $qRow['parent_id'];
        if ($parent_id != 0) {
            //$return[] = $parent_id;
            echo $parent_id; // geting parent id as 432 
            $return[] = $parent_id;
            $a = has_parent($parent_id, $con);
        }else{
            return $parent_id;
        }

    return $return;
}

$marray =  folder_has_parent($parent ,$con);
print_r($marray); // getting last array only

Array
(
    [0] => 4
)

预期产出:

Array
(
    [0] => 4
    [1] => 3
    [3] => 2
)

2 个答案:

答案 0 :(得分:0)

当您在行$return中调用递归函数时,您始终会覆盖$return = array();。所以你应该尝试使$return成为一个全局变量,因此它不再属于函数范围。

function has_parent($parent,$con) {
    global $return; // global variable
    $selectparent = "select * from table where id = $parent ";
    $qResult = mysqli_query($conobj,$selectparent);
    $qRow = mysqli_fetch_assoc($qResult);
    $parent_id = $qRow['parent_id'];
    if ($parent_id != 0) {
        //$return[] = $parent_id;
        echo $parent_id; // geting parent id as 432 
        $return[] = $parent_id;
        $a = has_parent($parent_id, $con);
    }else{
        return $parent_id;
    }

    return $return;
}

$return  = array(); // setting the global variable
$marray =  folder_has_parent($parent ,$con);
print_r($marray);

答案 1 :(得分:0)

由于array_merge,这可能会很慢,但它会按照您希望的顺序返回数组。

function has_parent($parent,$con) {
    $return  = array(); 
    $selectparent = "select * from table where id = $parent ";
    $qResult = mysqli_query($conobj,$selectparent);
    $qRow = mysqli_fetch_assoc($qResult);
    $parent_id = $qRow['parent_id'];
    if ($parent_id != 0) {
        $return = [ $parent_id ];
        $return = array_merge( $return, has_parent($parent_id, $con) );
    }    
    return $return;
}

这可能会更快,但结果会颠倒

function has_parent($parent,$con) {
    $return  = array(); 
    $selectparent = "select * from table where id = $parent ";
    $qResult = mysqli_query($conobj,$selectparent);
    $qRow = mysqli_fetch_assoc($qResult);
    $parent_id = $qRow['parent_id'];
    if ($parent_id != 0) {
        $return = has_parent($parent_id, $con);
        $return[] = $parent_id;
    }
    return $return;
}

当然,与查询时间相比,数组复制所需的时间可以忽略不计,但我更喜欢使用第二个变体,然后运行array_reverse。它更具可读性(我认为)。

更新:此功能可以轻松地重写为迭代功能。迭代函数更容易理解(品味),它可能更好(至少需要更少的memmory)然后递归。

function has_parent($parent,$con) {
    $return  = array(); 
    do{ 
       $selectparent = "select * from table where id = $parent ";
       $qResult = mysqli_query($conobj,$selectparent);
       $qRow = mysqli_fetch_assoc($qResult);
       $parent_id = $qRow['parent_id'];
       if( $parent_id != 0) {
          $return[] = $parent_id;
          $parent = $parent_id;
       }
    } while( $parent_id != 0 );
    return $return;
}