致敬,代码长老们,
我正在寻求掌握 PHP 的咒语,现在需要你的帮助来杀死一头强大的野兽。
我正在用 PHP 制作一个 REST API。其中一个函数是 GET,它返回目录中的 png 列表。但它不是返回一个数组,而是返回多个数组(每次迭代一个?)。
我想:
["1.png","2.png","3.png"]
但我得到:
["1.png"]["1.png","2.png"]["1.png","2.png","3.png"]
我表现出我对蔑视和羞辱的可怜功能:
function getPics() {
$pic_array = Array();
$handle = opendir('/srv/dir/pics');
while (false !== ($file = readdir($handle))) {
if ($file!= "." && $file!= ".." &&!is_dir($file)) {
$namearr = explode('.',$file);
if ($namearr[count($namearr)-1] == 'png') $pic_array[] = $file;
}
echo json_encode($pic_array);
}
closedir($handle);
}