Adam
Adam

Reputation: 20882

PHP - looping through an array of JSON Variables

I have the following array that is in PHP (this is a print_r() output).

I'm currently struggling to loop through this data - need to be able to process each part and access the values in each array item. How can I do this.

I've tried the following unsuccessfully...

foreach (array as $key => $value) {
 echo $key;
}

enter image description here

Upvotes: 2

Views: 338

Answers (2)

Timothy
Timothy

Reputation: 4650

You have an array of objects, so try something like this:

<?php
foreach ($array as $value) {
    echo $value->userid;
    echo $value->action;
    echo $value->photo_name;
}

You don't need the $key since you're not using it in the loop. Each iteration will put the object in the $value variable, on which you can access it's properties.

Upvotes: 4

Lucas Lim
Lucas Lim

Reputation: 515

Try this. Since you have an array of objects, you should be able to access each object property using ->

foreach($array as $value) {
    echo $value -> userid;
}

It should echo out all the user id in that array of objects

Upvotes: 4

Related Questions