parse nordpool nested JSON data to php string using decoded_json

Viewed 12

The goal here is to have the Name Printed followed by the values IE Tromsø: 33,28 30,00

Im very novice at this and have been struggling with this one for a few days trying to lookup nested PHP examples and just gives me an error

<?php
 
$people_json = file_get_contents('https://www.nordpoolgroup.com/api/marketdata/page/10?currency=,,,EUR');
 
$decoded_json = json_decode($people_json, true);
 
$data = $decoded_json['data'];

$filter = "Tromsø";

foreach($row as $Rows)  {
 
    echo( $filter.": " );
    
    foreach($Columns as $row['Columns'])  { 
        if($filter==$Columns['Name'])
        {
            echo($Columns['Value']. " ");
        }
    }
    
 }

?>```

1 Answers

Your foreach statements are the wrong way around; it should be

foreach ($array as $value)

where you have

foreach ($value as $array)

Once you correct that, your code works fine (with some minor cleanup):

$decoded_json = json_decode($people_json, true);
$data = $decoded_json['data'];

$filter = "Tromsø";
echo( $filter.": " );

foreach ($data['Rows'] as $row) {
    foreach ($row['Columns'] as $column) { 
        if ($filter==$column['Name']) {
            echo $column['Value']. " ";
        }
    }
}

Output (for the first two rows of the data):

Tromsø: 33,28 30,00 

Demo on 3v4l.org

Related