Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get individual fields from JSON in PHP

Tags:

json

php

My JSON looks like this. How do I get a specific field, e.g. "title" or "url"?

{
 "status":1,
    "list":
        {
         "204216523":
            {"item_id":"204216523",
             "title":"title1",
             "url":"url1",
            },
        "203886655":
            {"item_id":"203886655",
             "title":"titl2",
             "url":"url2",
            }
        },"since":1344188496,
  "complete":1
 }

I know $result = json_decode($input, true); should be used to get parsable data in $result, but how do I get individual fields out of $result? I need to run through all the members (2 in this case) and get a field out of it.

like image 869
hari Avatar asked Aug 10 '12 17:08

hari


1 Answers

json_decode() converts JSON data into an associative array. So to get title & url out of your data,

foreach ($result['list'] as $key => $value) {
    echo $value['title'].','.$value['url'];
}
like image 195
abhshkdz Avatar answered Oct 12 '22 04:10

abhshkdz