3v4l.org

run code in 300+ PHP versions simultaneously
<?php $input = array ( 'expanded' => 1, 'key' => 'root_1', 'title' => 'root', 'children' => array ( 0 => array ( 'folder' => 1, 'key' => 34, 'title' => 'YAY PROJECTS' ), 1 => array ( 'expanded' => 1, 'folder' => 1, 'key' => 6, 'title' => 'Grand Designs Episodes', 'children' => array ( 0 => array ( 'folder' => 1, 'key' => 8, 'title' => 'AU Episodes' ), 1 => array ( 'expanded' => 1, 'folder' => 1, 'key' => 7, 'title' => 'UK Episodes', 'children' => array ( 0 => array ( 'key' => 9, 'title' => 'Start something' ), 1 => array ( 'key' => 2, 'title' => 'Grand Designs Season 10' ), ), ) ) ), 2 => array ( 'expanded' => 1, 'folder' => 1, 'key' => 5, 'title' => 'Animations', 'children' => array ( 0 => array ( 'key' => 4, 'title' => 'Futurama Episode 191' ), 1 => array ( 'key' => 3, 'title' => 'Miniscule Series 5 Ep1' ), 2 => array ( 'key' => 1, 'title' => 'The Simpsons Episode 459' ) ) ), 3 => array ( 'folder' => 1, 'key' => 11, 'title' => 'Test Folder' ), 4 => array ( 'folder' => 1, 'key' => 10, 'title' => 'Testing' ) ) ); function generateParentIds(&$input, $parentId = 0) { $input['parent_id'] = $parentId; if (isset($input['children'])) { foreach ($input['children'] as &$child) { generateParentIds($child, $input['key']); } } } generateParentIds($input); print_r($input);
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.26, 8.1.0 - 8.1.13
Array ( [expanded] => 1 [key] => root_1 [title] => root [children] => Array ( [0] => Array ( [folder] => 1 [key] => 34 [title] => YAY PROJECTS [parent_id] => root_1 ) [1] => Array ( [expanded] => 1 [folder] => 1 [key] => 6 [title] => Grand Designs Episodes [children] => Array ( [0] => Array ( [folder] => 1 [key] => 8 [title] => AU Episodes [parent_id] => 6 ) [1] => Array ( [expanded] => 1 [folder] => 1 [key] => 7 [title] => UK Episodes [children] => Array ( [0] => Array ( [key] => 9 [title] => Start something [parent_id] => 7 ) [1] => Array ( [key] => 2 [title] => Grand Designs Season 10 [parent_id] => 7 ) ) [parent_id] => 6 ) ) [parent_id] => root_1 ) [2] => Array ( [expanded] => 1 [folder] => 1 [key] => 5 [title] => Animations [children] => Array ( [0] => Array ( [key] => 4 [title] => Futurama Episode 191 [parent_id] => 5 ) [1] => Array ( [key] => 3 [title] => Miniscule Series 5 Ep1 [parent_id] => 5 ) [2] => Array ( [key] => 1 [title] => The Simpsons Episode 459 [parent_id] => 5 ) ) [parent_id] => root_1 ) [3] => Array ( [folder] => 1 [key] => 11 [title] => Test Folder [parent_id] => root_1 ) [4] => Array ( [folder] => 1 [key] => 10 [title] => Testing [parent_id] => root_1 ) ) [parent_id] => 0 )

preferences:
56.59 ms | 421 KiB | 5 Q