php数组合并

php数组合并,php,arrays,merge,Php,Arrays,Merge,我在这行上遇到一个错误: $ret=array_merge($ret,preg_ls($path."/".$e,$rec,$pat)); 错误是:数组\合并参数2不是数组 我不知道该怎么解决这个问题 多谢各位 function preg_ls($path=".", $rec=false, $pat="/.*/") { // it's going to be used repeatedly, ensure we compile it for speed. $pat=preg_re

我在这行上遇到一个错误:

$ret=array_merge($ret,preg_ls($path."/".$e,$rec,$pat));
错误是:数组\合并参数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);
}
PHP中的数组不是JSON。这是一个数组。只需返回$ret

如果您需要的是数组,而不是json_encode提供的字符串,那么应该返回数组

另外,您使用的是echo,而不是return。echo打印到stdout或HTML主体,这取决于PHP环境,尽管它们是一个相同的环境,只是使用重定向和不同的环境来处理

return将导致函数将其返回值传递给调用者,这通常与预期的一样,传递到变量或另一个函数中;如果没有返回值,函数将始终返回NULL。

PHP中的数组不是JSON。这是一个数组。只需返回$ret

如果您需要的是数组,而不是json_encode提供的字符串,那么应该返回数组

另外,您使用的是echo,而不是return。echo打印到stdout或HTML主体,这取决于PHP环境,尽管它们是一个相同的环境,只是使用重定向和不同的环境来处理


return将导致函数将其返回值传递给调用者,这通常与预期的一样,传递到变量或另一个函数中;如果没有返回值,函数将始终返回NULL。

如果preg\u ls返回json字符串,则无法将其传递给array\u merge,因为它需要一个数组……json\u encode提供一个字符串,而不是数组。return$ret;与echo json_encode不同,$retpreg_ls不返回任何内容,而数组_merge需要一个数组。如果preg_ls返回一个json字符串,则无法将其传递给数组_merge,因为它需要一个数组。$json_encode提供一个字符串,而不是数组。return$ret;而不是echo json\u encode$retpreg\u ls不返回任何内容,array\u merge需要一个数组