1

我有一个函数,我可以通过回显获得所有正确的值,但是当我无法弄清楚如何返回整个数组时。有人可以帮我弄清楚获得所有儿童用户吗?

    $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);
        }
    }
4

3 回答 3

1

您需要通过参考Docs 传递

function getchildren($array, &$totalarray)
                             ^

用法:

$useridarray[] = $userid;
getchildren($useridarray, $useridarray);
var_dump($useridarray); # your result
于 2012-05-03T15:57:27.417 回答
0

您需要先添加退货

    mysql_close();
return getchildren($newchildren, $totalarray);
于 2012-05-03T15:57:31.023 回答
0

我不熟悉 PHP 语法,但您可能希望实现“停止条件”来返回结果。因此,在您的实例中,您似乎想要 getChildren 返回 localResults + recursive getChildren 结果的东西,这些结果在您通过 foreach 循环时逐渐总计。

于 2012-05-03T16:04:21.897 回答