【发布时间】:2012-02-18 12:09:30
【问题描述】:
致敬,代码长老,
我正在寻求掌握 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);
}
【问题讨论】: