我有一個函數,可以通過echo獲取所有正確的值,但是當我無法弄清楚如何獲取整個數組時返回。有人能幫我找出所有的小孩用戶嗎?從遞歸函數返回一個值
$useridarray[] = $userid;
getchildren($useridarray, $useridarray);
function getchildren($array, $totalarray)
{
foreach($array as $arr)
{
$db_name = "dbname";
$connection = @mysql_connect("localhost", "username", "password") or die(mysql_error());
$db = @mysql_select_db($db_name, $connection) or die(mysql_error());
$sql = "
select *
from users
where creator = '$arr'
";
$result = @mysql_query($sql, $connection) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$newchildren[] = $row['id'];
$totalarray[] = $row['id'];
//echo $row['id'] . ' ';
}
mysql_close();
getchildren($newchildren, $totalarray);
}
}
只是在實際和性能方面,我會創建一個函數來創建連接並選擇數據庫,並將這些細節傳遞給您的函數。將減少創建和關閉連接的開銷。 –