0

我有以下代码:

$images = array();
  foreach ($media->data as $data) {
    $images['src'] = $data->images->thumbnail->url;
    $images['user'] = $data->user->username;
    $images['time'] = $data->created_time;
  }

  echo json_encode(array(
    'next_id' => $pagination->next_page,
    'images'  => array('src' => $images['src'], 'user' => $images['user'], 'time' => $images['time'])

  ));

我希望它显示所有字段,但它只输出一个。如何让 Ccn 显示 json 输出中的所有字段?

谢谢你。

4

2 回答 2

8

您需要图像数组,而不是单个图像:

foreach ($media->data as $data) {
    $image=array();
    $image['src'] = $data->images->thumbnail->url;
    $image['user'] = $data->user->username;
    $image['time'] = $data->created_time;
    $images[]=$image;
  }

其他可能的语法是:

foreach ($media->data as $data) {
    $images[]=array(
    'src'  => $data->images->thumbnail->url,
    'user' => $data->user->username,
    'time' => $data->created_time
   );
  }

json 编码部分也应该改变:

 echo json_encode(array(
    'next_id' => $pagination->next_page,
    'images'  => $images)
 );
于 2013-08-05T19:34:55.573 回答
0
$images = array();
foreach ($media->data as $data) {
  $images[]=array(
    'src'  => $data->images->thumbnail->url,
    'user' => $data->user->username,
    'time' => $data->created_time
  );
}

echo json_encode(array(
  'next_id' => $pagination->next_page,
  'images'  => $images
));
于 2013-08-05T19:40:52.720 回答