3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ [ 'id' => 1, 'parentId' => "" ], [ 'id' => 2, 'parentId' => 1 ], [ 'id' => 3, 'parentId' => 2 ] ]; $entriesById = array_column($array, null, 'id'); $entriesWithAncestors = array_reduce( $entriesById, static function (array $result, array $entry) use ($entriesById): array { $result[$entry['id']] = $entry + ['ancestors' => []]; $parentId = $entry['parentId']; while (isset($result[$parentId])) { $result[$entry['id']]['ancestors'][] = $result[$parentId]; $parentId = $entriesById[$parentId]['parentId'] ?? null; } return $result; }, [] ); print_r($entriesWithAncestors);
Output for git.master, git.master_jit, rfc.property-hooks
Array ( [1] => Array ( [id] => 1 [parentId] => [ancestors] => Array ( ) ) [2] => Array ( [id] => 2 [parentId] => 1 [ancestors] => Array ( [0] => Array ( [id] => 1 [parentId] => [ancestors] => Array ( ) ) ) ) [3] => Array ( [id] => 3 [parentId] => 2 [ancestors] => Array ( [0] => Array ( [id] => 2 [parentId] => 1 [ancestors] => Array ( [0] => Array ( [id] => 1 [parentId] => [ancestors] => Array ( ) ) ) ) [1] => Array ( [id] => 1 [parentId] => [ancestors] => Array ( ) ) ) ) )

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:
115.45 ms | 411 KiB | 5 Q