我在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;
}
答案 0 :(得分: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;
}
的exaple:
$myArr = getResponsesRecursive('table', 0);
$myArr = arrary(
array(
'id' => 1,
'table' => 'table-name',
'responses' => array(
...
)
)
);
答案 1 :(得分: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;
}
?>