3v4l.org

run code in 300+ PHP versions simultaneously
<?php $jsonData = '[{ "brand": {"id": "fe877b45-8620-453a-8805-63f0cbd80752", "name": "No Brand", "slug": "no-brand", "description": "null"}, "options": [{"id": "324af955-1aa9-42ea-be6e-bb4e5623a97a", "parent_id": "null", "name": "Need Insurance?"}, {"id": "73298c18-4ccc-4138-afa5-71d3d00dff9b", "parent_id": "null", "name": "Color", "slug": "color"}], "rating": [], "tags": [{"id": "8a31ee4c-3302-4357-9686-bd4308bbf39f", "name": "options", "slug": "options", "photo": "null"}], "variations": [{"id": "3bf5aeb9-9da2-4fb1-a3d2-f89eb75839c3", "parent_id": "null", "name": "Ram", "slug": "ram", "photo": "null"}, {"id": "e5c70766-a558-4539-b41f-77f72c819a7c", "parent_id": "null", "name": "cpu", "slug": "cpu", "photo": "null"}, {"id": "e63ac831-f595-4889-83d2-a5be65734758", "parent_id": "null", "name": "Monitor", "slug": "monitor"} ] }, { "brand": {"id": "fe877b45-8620-453a-8805-63f0cbd80752", "name": "No Brand", "slug": "no-brand", "description": null}, "options": [{"id": "522da418-eb3f-43e9-9392-63c941842a52", "parent_id": null, "name": "Color", "slug": "color-3"}], "rating": [], "tags": [], "variations": [{"id": "8e9a26c5-2ee4-4d86-9244-a10596d67fea", "parent_id": null, "name": "Cpu", "slug": "cpu-3", "photo": null}] } ] '; $jsonDataArr =json_decode($jsonData); $data = array(); foreach($jsonDataArr as $key => $items){ foreach($items as $innerKey => $eachItem){ if(!isset($data[$innerKey])){ $data[$innerKey] = array(); if(is_array($eachItem)){ foreach($eachItem as $each) if(!empty($each)) $data[$innerKey][] = (array) $each; }else{ if(!empty($eachItem)) $data[$innerKey][] = (array) $eachItem; } }else{ if(is_array($eachItem)){ foreach($eachItem as $each) if(!empty($each)) $data[$innerKey][] = (array) $each; }else{ if(!empty($eachItem)) $data[$innerKey][] = (array) $eachItem; } } } } foreach($data as $key => $val){ foreach($val as $l => $item){ $index = trim(strtolower($item['name'])); $data[$key][$index] = $item; unset($data[$key][$l]); } } foreach($data as $key => $val){ $data[$key]=array_values($val); } print_r($data);
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.13
Array ( [brand] => Array ( [0] => Array ( [id] => fe877b45-8620-453a-8805-63f0cbd80752 [name] => No Brand [slug] => no-brand [description] => ) ) [options] => Array ( [0] => Array ( [id] => 324af955-1aa9-42ea-be6e-bb4e5623a97a [parent_id] => null [name] => Need Insurance? ) [1] => Array ( [id] => 522da418-eb3f-43e9-9392-63c941842a52 [parent_id] => [name] => Color [slug] => color-3 ) ) [rating] => Array ( ) [tags] => Array ( [0] => Array ( [id] => 8a31ee4c-3302-4357-9686-bd4308bbf39f [name] => options [slug] => options [photo] => null ) ) [variations] => Array ( [0] => Array ( [id] => 3bf5aeb9-9da2-4fb1-a3d2-f89eb75839c3 [parent_id] => null [name] => Ram [slug] => ram [photo] => null ) [1] => Array ( [id] => 8e9a26c5-2ee4-4d86-9244-a10596d67fea [parent_id] => [name] => Cpu [slug] => cpu-3 [photo] => ) [2] => Array ( [id] => e63ac831-f595-4889-83d2-a5be65734758 [parent_id] => null [name] => Monitor [slug] => monitor ) ) )

preferences:
134.1 ms | 413 KiB | 5 Q