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

preferences:
36.36 ms | 405 KiB | 5 Q