Erez.Matrix Erez.Matrix - 7 months ago 15
PHP Question

How to write a recursive foreach display function in PHP for a large multidimensional JSON file

I am rather a PHP novice and I already know how to write a foreach loop and nest IF statements for every dimension.

Problems start when a large, multi-dimiensional JSON comes along. It would be counter productive to write infinite IF statements so i'm trying to find a recursive solution to display any future JSON files, regardless of how deep they are.

On every google/stack search I've performed I've found questions that are pretty far since everyone asks the question that is relevant to their own code and i'm just trying to find a clear and non cluttered answer to this.

I was hoping if anyone here would be willing to lend a guiding hand in writing this function, I have no idea where to begin :/

Here is a JSON file from the devil himself:

{
"_id": "572b0a2c5924669b98a31791",
"index": 0,
"guid": "34989172-83cb-466a-87f6-76bbf374eff3",
"isActive": true,
"balance": "$2,859.31",
"picture": "http://placehold.it/32x32",
"age": 28,
"eyeColor": "blue",
"name": "Compton Wells",
"gender": "male",
"company": "NETPLAX",
"email": "comptonwells@netplax.com",
"phone": "+1 (932) 505-3240",
"address": "452 George Street, Stollings, Iowa, 5009",
"about": "Aliqua id nostrud adipisicing qui non pariatur enim nisi aliqua Lorem pariatur tempor ullamco fugiat. Pariatur velit cillum sunt in mollit dolor ex ullamco ut proident. Id Lorem sit cupidatat sunt deserunt. Exercitation magna excepteur incididunt proident minim mollit fugiat ad officia magna laboris enim labore veniam. Id cillum dolore ad non cupidatat aute labore enim esse et consequat amet. Exercitation excepteur anim cillum nisi est aliquip pariatur eu. Irure dolore laboris eiusmod qui Lorem consectetur enim nisi fugiat.\r\n",
"registered": "2015-11-10T01:03:21 -02:00",
"latitude": 42.546775,
"longitude": -164.862723,
"tags": [
"occaecat",
"veniam",
"et",
"eu",
"nulla",
"dolor",
"labore"
],
"friends": [
{
"id": 0,
"name": "Francisca Merrill"
},
{
"id": 1,
"name": "Jimmie Baxter"
},
{
"id": 2,
"name": "Essie Campbell"
}
],
"greeting": "Hello, Compton Wells! You have 4 unread messages.",
"favoriteFruit": "strawberry"
},
{
"_id": "572b0a2ca3c188d22631ce56",
"index": 1,
"guid": "61631ddc-6ffc-488d-a33b-96d6543256d1",
"isActive": true,
"balance": "$3,328.73",
"picture": "http://placehold.it/32x32",
"age": 32,
"eyeColor": "blue",
"name": "Campbell Steele",
"gender": "male",
"company": "HELIXO",
"email": "campbellsteele@helixo.com",
"phone": "+1 (944) 532-3258",
"address": "662 Taylor Street, Fairhaven, Marshall Islands, 7101",
"about": "Ex reprehenderit veniam id aliqua fugiat id. Non qui qui velit consectetur ipsum et cillum excepteur culpa proident amet. Quis cillum eiusmod ad minim anim amet occaecat excepteur.\r\n",
"registered": "2014-05-01T11:43:54 -03:00",
"latitude": -70.879773,
"longitude": -91.252999,
"tags": [
"eu",
"aliqua",
"dolor",
"sint",
"tempor",
"Lorem",
"exercitation"
],
"friends": [
{
"id": 0,
"name": "Maryellen Castro"
},
{
"id": 1,
"name": "Beatrice Thomas"
},
{
"id": 2,
"name": "Megan Fleming"
}
],
"greeting": "Hello, Campbell Steele! You have 5 unread messages.",
"favoriteFruit": "apple"
},
{
"_id": "572b0a2ce75622f7763c0a4a",
"index": 2,
"guid": "96afcfc5-447e-4b86-9cf3-a31da0cb67a0",
"isActive": false,
"balance": "$2,871.94",
"picture": "http://placehold.it/32x32",
"age": 24,
"eyeColor": "brown",
"name": "Loretta Roth",
"gender": "female",
"company": "FROLIX",
"email": "lorettaroth@frolix.com",
"phone": "+1 (903) 507-2688",
"address": "244 Freeman Street, Bluetown, Colorado, 7778",
"about": "Enim in et sunt ea sint proident. Consequat ad proident adipisicing voluptate. Veniam est consequat ut aliqua adipisicing culpa nulla ex aliquip Lorem enim eiusmod voluptate sint. Magna enim nulla cillum irure qui occaecat aliqua tempor sint pariatur aliquip officia nostrud. Non aliqua aliqua minim magna do aliqua. Mollit consectetur exercitation aliqua tempor cupidatat.\r\n",
"registered": "2014-05-10T09:54:48 -03:00",
"latitude": 5.431549,
"longitude": 116.259865,
"tags": [
"non",
"anim",
"in",
"elit",
"consequat",
"do",
"dolor"
],
"friends": [
{
"id": 0,
"name": "Heidi Holman"
},
{
"id": 1,
"name": "Cindy Fernandez"
},
{
"id": 2,
"name": "Maura Edwards"
}
],
"greeting": "Hello, Loretta Roth! You have 5 unread messages.",
"favoriteFruit": "banana"
},
{
"_id": "572b0a2cdaf81b24bbef848b",
"index": 3,
"guid": "02d79968-7eef-4600-9553-fa035ef196f1",
"isActive": false,
"balance": "$2,691.37",
"picture": "http://placehold.it/32x32",
"age": 24,
"eyeColor": "blue",
"name": "Beasley Strong",
"gender": "male",
"company": "VURBO",
"email": "beasleystrong@vurbo.com",
"phone": "+1 (902) 482-3914",
"address": "151 Apollo Street, Matheny, Utah, 1267",
"about": "Ea nisi exercitation aliqua dolor laboris non reprehenderit nulla occaecat qui fugiat. Ipsum cillum eiusmod tempor elit voluptate quis esse. Pariatur aute cillum commodo et est anim nisi sit nostrud do ea sit. In adipisicing officia et velit sunt anim irure culpa sint enim velit cillum. Id est id labore sunt mollit duis veniam duis duis pariatur nulla amet culpa velit. Deserunt reprehenderit in dolor ut pariatur non commodo sit cillum eiusmod aliquip quis. Consequat non velit cupidatat magna pariatur.\r\n",
"registered": "2016-02-10T09:24:38 -02:00",
"latitude": 61.905235,
"longitude": 9.908656,
"tags": [
"nostrud",
"laborum",
"non",
"non",
"tempor",
"nostrud",
"nisi"
],
"friends": [
{
"id": 0,
"name": "Morris Lucas"
},
{
"id": 1,
"name": "Jeannette Michael"
},
{
"id": 2,
"name": "Black Duke"
}
],
"greeting": "Hello, Beasley Strong! You have 9 unread messages.",
"favoriteFruit": "apple"
},
{
"_id": "572b0a2c13804da61c1927b9",
"index": 4,
"guid": "6f15b064-d21a-4979-a380-e96a8d931258",
"isActive": true,
"balance": "$3,235.01",
"picture": "http://placehold.it/32x32",
"age": 38,
"eyeColor": "brown",
"name": "Byrd Nieves",
"gender": "male",
"company": "FIBEROX",
"email": "byrdnieves@fiberox.com",
"phone": "+1 (901) 496-3284",
"address": "528 Hart Street, Hatteras, Nevada, 4179",
"about": "Consectetur sint consequat cupidatat ullamco qui consectetur nostrud ex cillum aliqua velit. Irure Lorem tempor nostrud labore est enim aute ex irure fugiat exercitation. Non eiusmod pariatur excepteur elit velit reprehenderit irure quis ea adipisicing sunt. Sunt consectetur cupidatat ad eiusmod pariatur Lorem aliqua exercitation laborum.\r\n",
"registered": "2014-03-13T06:40:39 -02:00",
"latitude": -88.232991,
"longitude": 15.675919,
"tags": [
"consectetur",
"aliqua",
"nisi",
"mollit",
"magna",
"consequat",
"ut"
],
"friends": [
{
"id": 0,
"name": "Strickland Bender"
},
{
"id": 1,
"name": "Manuela Holt"
},
{
"id": 2,
"name": "Esther Sutton"
}
],
"greeting": "Hello, Byrd Nieves! You have 5 unread messages.",
"favoriteFruit": "strawberry"
}

Answer

The solution is:

  • First decode your json data using json_decode() function, this will give you a multidimensional array.
  • Now recursively iterate the array using foreach loop and print all the (key, value) pairs.

So your code should be like this:

function process_jsonData($array){
    foreach($array as $key => $value){
        if(is_array($value)){
            process_jsonData($value);
        }else{
            echo $key . ": " . $value . "<br />";
        }
    }
}

// Suppose $json is your json data 
$jsonData = json_decode($json, true);
process_jsonData($jsonData);