Home > Software engineering >  Unable to get vlaues from JSON array
Unable to get vlaues from JSON array

Time:06-16

I've checked number of suggested thread here and tried the solution there but still unable to extract what I want from JSON array.

What I have so far is

function getData($offset = 0, $length = 99){

$url = "https://api.example.com/api.php?offset=$offset&length=$length";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $jsonData = curl_exec($ch);
        $response = json_decode($jsonData);
        
    return $response;       
}

$allData = array();

do {
    $offset = $response->response->total ?: 0;
    $response = getData($offset);

    foreach($response->response as $row){
         $allData[] = $row;
         $urlData = $row->data;
         
         if (!empty($urlData)) {
             array_push($allData, $urlData);
         }
    }
  
} while($response->response->total == 1);

This piece of code return array like print_r($response->response->data);

stdClass Object
(
    [response] => stdClass Object
        (
            [total] => 56
            [frequency] => annual
            [data] => Array
                (
                    [0] => stdClass Object
                        (
                            [period] => 2021
                            [activityId] => 1
                            [activityName] => Production
                            [countryRegionTypeName] => Country
                            [unit] => TBPD
                            [value] => 0
                        )

                    [1] => stdClass Object
                        (
                            [period] => 2021
                            [activityId] => 1
                            [activityName] => Production
                            [countryRegionTypeName] => Country
                            [unit] => TBPD
                            [value] => 0
                        )

                    [2] => stdClass Object
                        (
                            [period] => 2021
                            [activityId] => 1
                            [activityName] => Production
                            [countryRegionTypeName] => Country
                            [unit] => TBPD
                            [value] => 0
                        )
                )
         )
    )

Now I'm trying to access the data [period], [value], etc but when I try with

$response->response->data->value

Shows blank page.

There is no problem when I do print_r($response->response); or print_r($response->response->data); but shows nothing when I go beyond data e.g. print_r($response->response->data->value);

May be is trivial but what is wrong here?

CodePudding user response:

As you see, data is an array of objects, so you should access it as an array.

  • Related