0

我对 PHP 中的递归函数有疑问。我调用函数并从数据库中获取行,然后将行放入数组中,然后再次调用函数以获取与该行连接的其他行......问题是,array_unshift 只保存第一行而其他不保存。问题是什么。谢谢您的回答。

    public static function nahrejZpravy($responseTable, $responseId){
    $return = Array();
    $query = "SELECT * FROM Zpravy WHERE responseTable = '$responseTable' AND responseId = '$responseId'";
    $result = query($query);
    while($row = mysql_fetch_assoc($result)){
        array_unshift($return, $row);
        Zpravy::nahrejZpravy('Zpravy', $row['id']);
    }  
    return $return;
}
4

2 回答 2

1
<?php

function getResponsesRecursive($responseTable, $responseId) {
    $responses = array();
    $query = "SELECT * FROM Zpravy WHERE responseTable = '$responseTable' AND responseId = '$responseId'";
    $result = mysql_query($query);
    while ( $row = mysql_fetch_assoc($result) ) {
        $row['responses'] = getResponsesRecursive($row['table'], $row['id']);
        $responses[] = $row;
    }
    return $responses;
}
?>
于 2013-08-26T13:16:33.020 回答
1

你实际上需要这样的东西:

<?php

function getResponsesRecursive($responseTable, $responseId) {
    $responses = array();
    $query = "SELECT * FROM Zpravy WHERE responseTable = '$responseTable' AND responseId = '$responseId'";
    $result = query($query);
    while ( $row = mysql_fetch_assoc($result) ) {
        $row['responses'] = getResponsesRecursive($row['table'], $row['id']);
        $responses[] = $row;
    }
    return $responses;
}

例如:

$myArr = getResponsesRecursive('table', 0);

$myArr = arrary(
    array(
        'id'    =>  1,
        'table' =>  'table-name',
        'responses' =>  array(
            ...
        )
    )
);
于 2013-08-26T12:45:10.470 回答