yuli chika
yuli chika

Reputation: 9221

json foreach problem

{
   "data": [
      {
         "caption": "www.bollywoodtune.com",
         "type": "link",
         "created_time": "2011-01-17T07:23:02+0000",
         "updated_time": "2011-01-17T07:23:02+0000"
      },
...
 ]
}

here is the json form, how to make a foreach? When I use

foreach ($data[0] as $result) {
...
}

it show Fatal error: Cannot use object of type stdClass as array in line foreach ($data[0] as $result) Thanks.

Upvotes: 0

Views: 518

Answers (3)

Shakti Singh
Shakti Singh

Reputation: 86346

decode the json data

$data= json_decode($data,true);
foreach ($data as $v)
  {
  }

Upvotes: 1

bharath
bharath

Reputation: 1233

you need to do this:

$data = json_decode($data, true);

foreach($data as $d)
{
  //stmts here
}

for more information about the parameters visit php manual for json

Upvotes: 2

Thai
Thai

Reputation: 11354

When you use json_decode, make sure that you pass true to the second operator.

For example,

$data = json_decode($json, true);

Normally, objects converted using json_decode will be stored as PHP objects which can't be iterated over. Passing true as the second argument makes json_decode convert objects to associative arrays instead.

Upvotes: 2

Related Questions