[英]php array_merge
I am getting an error on this line: 我在这条线上出现错误:
$ret=array_merge($ret,preg_ls($path."/".$e,$rec,$pat));
Error is: array_merge() Argument #2 is not an array 错误是:array_merge()参数2不是数组
I dont know know to solve this. 我不知道要解决这个问题。
Thank you. 谢谢。
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);
}
An Array
in PHP is not JSON. PHP中的Array
不是JSON。 It's an array. 这是一个数组。 Simply return $ret;
只需return $ret;
You should return the array if you are expecting an array, not a string (as json_encode
gives). 如果期望数组,而不是字符串(如json_encode
给出),则应返回数组。
Also, you are using echo
, not return
. 另外,您正在使用echo
,而不是return
。 echo
prints to either stdout
or the HTML body, depending on the PHP environment (though they are one and the same, just with redirects and a different environment to handle it). 根据PHP环境的不同, echo
打印到stdout
或HTML正文(尽管它们是相同的,只是带有重定向和不同的环境来处理它)。
return
will cause a function to pass its return value to the caller, as expected (usually into a variable or another function); return
将使函数按预期将其返回值传递给调用方(通常传递给变量或其他函数); without a return value, the function will always return NULL
. 没有返回值,该函数将始终返回NULL
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.