0

I have the following code:

$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'])

  ));

I want it to display all the fields but it only outputs one. How Ccn I make it display all the fields on json output?

Thank you.

1
  • You mean use all the images in $media->data? Commented Aug 5, 2013 at 19:35

2 Answers 2

8

You need array of images, not single image:

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;
  }

other possible syntax is:

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

json encode part also should be changed:

 echo json_encode(array(
    'next_id' => $pagination->next_page,
    'images'  => $images)
 );
Sign up to request clarification or add additional context in comments.

Comments

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
));

2 Comments

Do add some explanation on why this approach is better, though for you, the problem (or solution) might be readable in the code, the real answer is in explaining why this is an improvement. If textually improved, I'll +1 it :)
I'm not good know English for write explanation. Sorry. I'm just give solution. (and @eicto copyed it)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.