php array_merge

Ton*_*niq 1 php arrays merge

我在这一行收到错误:

$ret=array_merge($ret,preg_ls($path."/".$e,$rec,$pat));
Run Code Online (Sandbox Code Playgroud)

错误是:array_merge()参数#2不是数组

我不知道要解决这个问题.

谢谢.

function preg_ls($path=".", $rec=false, $pat="/.*/") {
    // it's going to be used repeatedly, ensure we compile it for speed.
    $pat=preg_replace("|(/.*/[^S]*)|s", "\\1S", $pat);
    //echo($pat);
    //Remove trailing slashes from path
    while (substr($path,-1,1)=="/") $path=substr($path,0,-1);
    //also, make sure that $path is a directory and repair any screwups
    if (!is_dir($path)) $path=dirname($path);
    //assert either truth or falsehoold of $rec, allow no scalars to mean truth
    if ($rec!==true) $rec=false;
    //get a directory handle
    $d=dir($path);
    //initialise the output array
    $ret=Array();
    //loop, reading until there's no more to read
    while (false!==($e=$d->read())) {
        //Ignore parent- and self-links
        if (($e==".")||($e=="..")) continue;
        //If we're working recursively and it's a directory, grab and merge
        if ($rec && is_dir($path."/".$e)) {
            $ret=array_merge($ret,preg_ls($path."/".$e,$rec,$pat));
            continue;
        }
        //If it don't match, exclude it
        if (!preg_match($pat,$e)) continue;
        //In all other cases, add it to the output array
        //echo($path."/".$e."<br/>");
        $ret[]=$path."/".$e;
    }
    //finally, return the array
    echo json_encode($ret);
}
Run Code Online (Sandbox Code Playgroud)

Ame*_*lia 6

一个Array在PHP是不是JSON.这是一个阵列.只是return $ret;

如果你期望一个数组,而不是一个字符串(如json_encode给出),你应该返回数组.

此外,你正在使用echo,而不是return.echo打印到stdoutHTML主体或HTML主体,具体取决于PHP环境(虽然它们是同一个,只是重定向和不同的环境来处理它).

return将导致函数将其返回值传递给调用者,如预期的那样(通常是变量或其他函数); 如果没有返回值,函数将始终返回NULL.