如何摆脱json处理的下方括号?
[{"success":true,"filename":"bialding_and_rebialding_plymouth02.jpg"},{"success":true,"filename":"bialding_and_rebialding_plymouth03.jpg"},{"success":true,"filename":"bialding_and_rebialding_plymouth04.jpg"}]
上面的结果由下面的类处理成一个数组,
function handle_upload($upload_directory)
{
# Loop the code according to the number of files.
for($i = 1; $i <= $this->total; $i++)
{
...
if ($this->file->save($upload_directory.$name_filtered.'.'.$file_extension , $i-1))
{
$message[] = array('success'=>true,'filename'=>$name_filtered.'.'.$file_extension);
}
else
{
$message[] = array('error'=> 'Could not save uploaded file.' . 'The upload was cancelled, or server error encountered');
}
}
return $message;
}
然后我使用json_encode
将数组转换为json格式,
$uploader = new uploader();
$result = $uploader->handle_upload('uploads/');
echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);
但我只在没有括号的结果中需要这个,
{"success":true,"filename":"bialding_and_rebialding_plymouth02.jpg"},{"success":true,"filename":"bialding_and_rebialding_plymouth03.jpg"},{"success":true,"filename":"bialding_and_rebialding_plymouth04.jpg"}
答案 0 :(得分:20)
str_replace(array('[', ']'), '', htmlspecialchars(json_encode($result), ENT_NOQUOTES));
?
答案 1 :(得分:1)
你可以通过在你的函数中执行以下操作来拉出json并返回它:
return $message[0]; // only json
或者您可以将其全部归还,然后将其删除:
$uploader = new uploader();
$result1 = $uploader->handle_upload('uploads/');
$result2 = $result[0];