3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ (object) ['id' => 1, 'category' => 'Pizza', 'product' => 'Large Pizza', 'complement_type' => 'Bread', 'option' => 'Brown bread'], (object) ['id' => 2, 'category' => 'Pizza', 'product' => 'Small Pizza', 'complement_type' => 'Bread', 'option' => 'White bread'], (object) ['id' => 3, 'category' => 'Pizza', 'product' => 'Small Pizza', 'complement_type' => 'Ingredients', 'option' => 'Olives'], (object) ['id' => 4, 'category' => 'Salads', 'product' => 'Green Salad', 'complement_type' => 'Extras', 'option' => 'Bacon'], (object) ['id' => 5, 'category' => 'Salads', 'product' => 'Cesars Salad', 'complement_type' => 'Extras', 'option' => 'Lettuce'], ]; $result = []; foreach ($array as $obj) { $result[$obj->category][$obj->product][$obj->complement_type] = $obj->option; } var_export($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.25, 8.4.1 - 8.4.12
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', ), ), )

preferences:
96.42 ms | 408 KiB | 5 Q