0

I have written and test my php code (not WordPress) it works well. But it doesn't work in WordPress. here's my snippet code (got error result):

function totalTrackLenght($tracklenght){
    global $row, $data, $tracklenght;
    foreach ($data->results as $row) {
        if(!empty($row->trackTimeMillis)){
            $tracklenght += $row->trackTimeMillis;
        }
    }
    return $tracklenght;
}

php file to display the result:

<li class="total-tr-lenght"><?php nextFunction(); ?></li>

I change foreach ($data->results as $row) to foreach ((array)$data->results as $row) is getting no result.

The JSON data obtained from https://itunes.apple.com/us/lookup?id=1434446667&entity=song

1 Answers1

0

Most likely your json is not stored as an associative array, if you're processing json with json_decode method, you should set second parameter as true - returned objects will be converted into associative arrays.

$data = json_decode($json, true) 

More info about json_decode

Marat Badykov
  • 824
  • 4
  • 8