3v4l.org

run code in 300+ PHP versions simultaneously
<?php $jsonString = '[{"id":1,"category":"Pizza","product":"Large Pizza","complement_type":"Bread","option":"Brown bread"},{"id":2,"category":"Pizza","product":"Small Pizza","complement_type":"Bread","option":"White bread"},{"id":3,"category":"Pizza","product":"Small Pizza","complement_type":"Ingredients","option":"Olives"},{"id":4,"category":"Salads","product":"Green Salad","complement_type":"Extras","option":"Bacon"},{"id":5,"category":"Salads","product":"Cesars Salad","complement_type":"Extras","option":"Lettuce"}]'; $array = json_decode($jsonString); $result = array(); foreach ($array as $row) { if (!isset($result[$row->category])) { $result[$row->category] = array(); } if (!isset($result[$row->category][$row->product])) { $result[$row->category][$row->product] = array(); } $result[$row->category][$row->product][$row->complement_type] = $row->option; } print_r($result);
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.5.0
Array ( [Pizza] => Array ( [Large Pizza] => Array ( [Bread] => Brown bread ) [Small Pizza] => Array ( [Bread] => White bread [Ingredients] => Olives ) ) [Salads] => Array ( [Green Salad] => Array ( [Extras] => Bacon ) [Cesars Salad] => Array ( [Extras] => Lettuce ) ) )
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.

preferences:
122.89 ms | 408 KiB | 5 Q