John Davidson

json - Better way to iterate multidimensional array of unknown complexity from API than a dozen foreach loops in PHP

0 comments
Message:


I am getting an array from an API that varies in number of levels but follows the same basic structure - here is a truncated sample as this particular repsonse is 25K lines:


{
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "Ordinary Income/Expenses"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"Rows": {},
"Summary": {
"ColData": [
{
"value": "Gross Profit"
},
{
"value": ""
}
]
},
"type": "Section"
},
{
"Header": {
"ColData": [
{
"value": "Income"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "40000 Sales Income",
"id": "31"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"Rows": {
"Row": [
{
"ColData": [
{
"value": "2022-01-24"
},
{
"value": "Invoice",
"id": "148774"
},
{
"value": "208232"
},
{
"value": "Hyatt:#211102",
"id": "7568"
},
{
"value": "JN",
"id": "4100000000000368107"
},
{
"value": "CAPTIVE AIRE"
},
{
"value": "11000 Accounts Receivable",
"id": "80"
},
{
"value": "38748.00"
},
{
"value": "38748.00"
}
],
"type": "Data"
},

I need to traverse the json, and where there is data in both [Header][ColData][value] AND [Header][ColData][id] extract the value, id (in this snippet "value": "40000 Sales Income", "id": "31") and the data that immediately follows the "value"/"id" in [Rows][Row][Rows][Row][ColData] (in this snippet starting with "ColData": [{"value": "2022-01-24"...)


[Rows][Row][Rows][Row][ColData] will have one to a few hundred subarrays. I can extract the data from the subarrays once they are found - it's just managing the varying depths of the array that is warping my brain.


[Rows][Row][Rows][Summary] can be discarded as well.


I have tried multiple foreach loops - but by time I get 5 or 6 levels deep it gets very confusing. The number of Header sections varies depending on the report type. The [Rows][Row] nesting is multiple layers deep... I'm sure there has to be a better way than nesting foreach loops...



My Answer:
{
"data": {
"id": 1,
"name": "John",
"children": [
{
"id": 2,
"name": "Jane",
"children": [
{
"id": 3,
"name": "Alice",
"children": []
},
{
"id": 4,
"name": "Bob",
"children": [
{
"id": 5,
"name": "Charlie",
"children": []
}
]
}
]
}
]
}
}


To iterate through this multidimensional array in PHP without knowing the exact depth of the array, you can use a recursive function. Here's an example of how you can achieve this:

php
function iterateArray($array) {
foreach ($array as $key => $value) {
if (is_array($value)) {
iterateArray($value);
} else {
echo "$key: $value\n";
}
}
}

$data = json_decode($json, true);
iterateArray($data);


This recursive function will iterate through each level of the array and output the key-value pairs. This way, you don't need to use a dozen foreach loops to iterate through the array.

Rate this post

4 of 5 based on 8548 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us