How to get individual fields from JSON in PHP

My JSON looks like this. How to get a specific field, for example. "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 what $result = json_decode($input, true);should be used to get the analyzed data in $result, but how can I get individual fields from $result? I need to run through all the members (in this case 2) and get a field from it.

+5
source share
3 answers

json_decode()converts JSON data to an associative array. To get the name and URL from your data,

foreach ($result['list'] as $key => $value) {
    echo $value['title'].','.$value['url'];
}
+6
source
echo $result['list']['204216523']['item_id']; // prints 204216523
+1
source

json_decode()translates your JSON data into array. Think of it as an associative array, because that is what it is.

+1
source

All Articles