我有一个函数,我可以通过回显获取所有正确的值,但是当我无法弄清楚如何返回整个数组时。 有人可以帮助我弄清楚获得所有儿童用户吗?
$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);
}
}
您需要通过引用文档传递:
function getchildren($array, &$totalarray)
^
用法:
$useridarray[] = $userid;
getchildren($useridarray, $useridarray);
var_dump($useridarray); # your result
您需要
在之前添加返回
mysql_close();
return getchildren($newchildren, $totalarray);
我不
熟悉PHP语法,但您可能希望实现"停止条件"以返回结果。因此,在您的实例中,似乎您想要一些东西,其中 getChildren 返回 localResults + 递归 getChildren 结果,这些结果在您通过 foreach 循环时以增量方式总计。