I need to loop through a multidimensional array and extract all the data:
PHP code:
//Loads the data and transforms it into an array
$json_data = json_decode(file_get_contents('data.json'));
//Loops through the array and returns the values
echo '<br>'.'Modal: ' .$json_data->list[0]->mode;
echo '<br>'.'Variation: ' .$json_data->list[0]->change;
foreach ($json_data->list[0]->expiry as $data) {
echo '<br>'.'Due date: ' .$data->cdexpiry;
echo '<br>'.'Value: ' .$data->vlexpiry;
}
JSON data:
{
"list": [
{
"expiry": [
{
"cdexpiry": "v130",
"vlexpiry": "33528.99"
},
{
"cdexpiry": "v230",
"vlexpiry": "21648.39"
},
{
"cdexpiry": "v150",
"vlexpiry": "185350.30"
}
],
"mode": "0401",
"change": "N"
},
{
"expiry": {
"cdexpiry": "v40",
"vlexpiry": "1091.46"
},
"mode": "1904",
"change": "N"
},
{
"expiry": {
"cdexpiry": "v770",
"vlexpiry": "1001.56"
},
"mode": "1904",
"change": "N"
},
{
"expiry": [
{
"cdexpiry": "v130",
"vlexpiry": "2084.08"
},
{
"cdexpiry": "v260",
"vlexpiry": "28362.31"
}
],
"mode": "0213",
"change": "N"
},
{
"expiry": {
"cdexpiry": "v210",
"vlexpiry": "296.78"
},
"mode": "0101",
"change": "N"
},
{
"expiry": [
{
"cdexpiry": "v245",
"vlexpiry": "1495.37"
},
{
"cdexpiry": "v248",
"vlexpiry": "1440.37"
}
],
"mode": "0217",
"change": "N"
}
],
"type": "8"
}
I’ve used a few for’s and if’s – but I’m missing something.
for ($i = 0; $i < count($json_data->list); $i++) {
if(isset($json_data->list[$i]->expiry))
Desired Outcome
Type | Value |
---|---|
Modal | 0401 |
Variation | N |
Due date | v130 |
Value | 33528.99 |
Due date | v230 |
Value | 21648.39 |
Due date | v150 |
Value | 185350.30 |
— | — |
Modal | 1904 |
Variation | N |
Due date | V40 |
Value | 1091.46 |
Due date | v770 |
Value | 1001.56 |
— | — |
Modal | 0213 |
Variation | N |
Due date | v130 |
Value | 2084.08 |
Due date | v260 |
Value | 28362.31 |
— | — |
Modal | 0101 |
Variation | N |
Due date | V210 |
Value | 296.78 |
— | — |
Modal | 0217 |
Variation | N |
Due date | V245 |
Value | 1495.37 |
Due date | V248 |
Value | 1440.37 |
This is the original XML query return file link
I need to extract all the data from the array to be able to manipulate it later. I count on the masters’ help!
2
Answers
That JSON has an irregular structure. Sure that is the actual data you receive? expiry is sometimes an array, sometimes an object. Very strange.
Take a look at this example to understand how to traverse it:
The output is:
Considering that strange structure of the JSON data you receive I really would recommend to try to have that structure fixed insteead of trying to use that structure. The unexpected varying formatting inside the JSON data makes it unnecessarily complex to process. I fail to see a reason for that. objects inside an API response should always have a strict and uniform structure.
Unfortunately, you have an inconsistent array structure, so not only do need to write loops to access each layer, you need to write a condition to accommodate the deviated subset structures.
Code: (Demo click the eye icon to render the html output)