在foreach中获得单一价值

时间:2018-10-03 05:12:53

标签: php wordpress php-5.6

我在foreach上使用json_encode,它返回多个值,我只希望在foreach完成后只返回一个值

 foreach($tree as $file) {
 $arr = array('success'=>'ok');
 echo json_encode($arr);
 }

{"success":"ok"}{"success":"ok"}{"success":"ok"}{"success":"ok"}{"success":"ok"}{"success":"ok"}

预期产量

{"success":"ok"}

1 个答案:

答案 0 :(得分:2)

执行以下操作:

foreach($tree as $file) {
 $arr = array('success'=>'ok');
}

echo json_encode($arr);

输出将与预期的一样: {"success":"ok"}