Home Ask Login Register

Developers Planet

Your answer is one click away!

Naman Sood February 2016

Select value from JSON using PHP

How to select data from bellow JSON code?

"response": {
"status": 1,
"httpStatus": 200,
"data": [
{
"offer_id": "8000",
"countries": {
"SG": {
"id": "702",
"code": "SG",
"name": "Singapore",
"regions": []
}
}

How could I select the words SG and SINGAPORE

Answers


Gwendal February 2016

You can use the json_decode function from php. http://php.net/manual/en/function.json-decode.php

For exemple :

<?php
$json = '{"response": {
    "status": 1,
    "httpStatus": 200,
    "data": [{
        "offer_id": "8000",
        "countries": {
            "SG": {
                "id": "702",
                "code": "SG",
                "name": "Singapore",
                "regions": []
            }
        }
    }]
}}';
$decode = json_decode($json);
var_dump($decode->response->data[0]->SG->name);

Post Status

Asked in February 2016
Viewed 1,989 times
Voted 14
Answered 1 times

Search




Leave an answer


Quote of the day: live life