3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = [ 'id' => 5, 'name' => 'Item 5', 'all_parents' => [ 'id' => 4, 'name' => 'Item 4', 'all_parents' => [ 'id' => 3, 'name' => 'Item 3', 'all_parents' => [ 'id' => 2, 'name' => 'Item 2', 'all_parents' => [ 'id' => 1, 'name' => 'Item 1', 'all_parents' => null ] ] ] ] ]; class Recursing { public function generateBreadcrumb($structure): array { return array_merge( empty($structure['all_parents']) ? [] : $this->generateBreadcrumb($structure['all_parents']), [['id' => $structure['id'], 'name' => $structure['name']]] ); } } $test = new Recursing; var_export($test->generateBreadcrumb($arr));
Output for 7.4.0 - 7.4.33, 8.0.1 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.27, 8.4.1 - 8.4.14
array ( 0 => array ( 'id' => 1, 'name' => 'Item 1', ), 1 => array ( 'id' => 2, 'name' => 'Item 2', ), 2 => array ( 'id' => 3, 'name' => 'Item 3', ), 3 => array ( 'id' => 4, 'name' => 'Item 4', ), 4 => array ( 'id' => 5, 'name' => 'Item 5', ), )

preferences:
127.95 ms | 407 KiB | 5 Q