Search code examples
getjsonjson

Selecting Data from JSON object using php


I have json data i want to select some data before i tell you please read the data carefully.

{
    "response": {
        "status": 1,
        "httpStatus": 200,
        "data": [{
            "offer_id": "6912",
            "Thumbnail": {
                "10116": {
                    "id": "10116",
                    "offer_id": "6912",
                    "display": "Icandytv_IN_Call-30-19-20-51).gif",
                    "thumbnail": "https:\/\/media.go2speed.org\/brand\/files\/mobvista\/6912\/thumbnails_100\/Icandytv_IN_Call(04-30-19-20-51).gif"
                }
            }
        }],
        "errors":[] ,
        "errorMessage": null
    }
}

From above Data i want to collect the value of thumbnail plese help me to find it out using PHP


Solution

  • Maybe that is an copy/paste problem, but your JSON-String is missing some }. But that is not the issue. If you want to access thumbnail (lowercase) in your json, try following:

    <?php
    
    $json = '{"response": {"status":1,"httpStatus":200,"data":[{"offer_id":"6912","Thumbnail":{"10116":{"id":"10116","offer_id":"6912","display":"Icandytv_IN_Call (04-30-19-20-51).gif","thumbnail":"https://media.go2speed.org/brand/files/mobvista/6912/thumbnails_100/Icandytv_IN_Call(04-30-19-20-51).gif"}}}],"errors":[],"errorMessage":null}}';
    
    // Make JSON accessible for PHP
    $data = json_decode($json);
    
    // Get access to Thumbnail object
    $thumbnail0 = $data->response->data[0]->Thumbnail;
    
    // Get access to thumbnail object "10116"
    $thumbnail_10116 = $thumbnail0->{"10116"};
    
    // Thumbnail Url
    $thumbnailUrl = $thumbnail_10116->thumbnail;
    
    echo $thumbnailUrl . "\n";
    
    // or in one swoop
    $thumbnailUrl2 = $data->response->data[0]->Thumbnail->{"10116"}->thumbnail;
    
    echo $thumbnailUrl2 . "\n";
    
    ?>
    

    Hope, that helps