3v4l.org

run code in 500+ PHP versions simultaneously
<?php $arr = ["categories" => [ [ 'id' => 368, 'name' => 'Aksesuar', 'parentId' => NULL, 'subCategories' => [ [ 'id' => 387, 'name' => 'Saat', 'parentId' => 368, 'subCategories' => [] ], [ 'id' => 394, 'name' => 'Şapka', 'parentId' => 368, 'subCategories' => [] ], [ 'id' => 396, 'name' => 'Takı & Mücevher', 'parentId' => 368, 'subCategories' => [ [ 'id' => 397, 'name' => 'Bileklik', 'parentId' => 396, 'subCategories' => [ [ 'id' => 1238, 'name' => 'Altın Bileklik', 'parentId' => 397, 'subCategories' => [] ] ] ] ] ], ] ] ]]; function recursive(string $needle, array $categories, $id): ?array { foreach ($categories as $cat) { if (array_key_exists($needle, $cat) && $cat[$needle] === $id) { // make sure that if correct category is found, // no subcategories will be included unset($cat['subCategories']); return $cat; } else { $subCat = recursive($needle, $cat['subCategories'], $id); // if the $subCat is not null, it indicates the category // with the correct id, or one of it's parent has been found if ($subCat !== null) { $cat['subCategories'] = $subCat; return $cat; } } } return null; } var_dump(recursive('id', $arr['categories'], 397));
Output for git.master, git.master_jit, rfc.property-hooks
array(4) { ["id"]=> int(368) ["name"]=> string(8) "Aksesuar" ["parentId"]=> NULL ["subCategories"]=> array(4) { ["id"]=> int(396) ["name"]=> string(17) "Takı & Mücevher" ["parentId"]=> int(368) ["subCategories"]=> array(3) { ["id"]=> int(397) ["name"]=> string(8) "Bileklik" ["parentId"]=> int(396) } } }

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
53.8 ms | 1478 KiB | 4 Q