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);

preferences:
24.25 ms | 406 KiB | 5 Q