3v4l.org

run code in 500+ PHP versions simultaneously
<?php function findWayPoints(array $lookup, $start, $end, array $path = []): ?array { if (isset($lookup[$start][$end])) { return $path; // full path found; no need to further recurse } foreach ($lookup[$start] ?? [] as $conn => $irrelevant) { $result = findWayPoints( array_diff_key($lookup, [$start => null]), // eliminate circular/infinite recursion $conn, // change starting node in next call $end, array_merge($path, [$conn]) // append current match to path ); if ($result !== null) { return $result; } } return null; // dead end; kill path } function buildLookup($nodes) { $lookup = []; foreach ($nodes as $k => [$connections]) { foreach (explode(' ', $connections) as $conn) { $lookup[$k][$conn] = true; $lookup[$conn][$k] = true; } } return $lookup; } $nodes = [ 'f' => ['d g'], 'b' => ['a d'], 'g' => ['i'], 'd' => ['c e'], 'i' => ['h'] ]; $lookup = buildLookup($nodes); echo json_encode(findWayPoints($lookup, 'h', 'f')); echo "\n---\n"; echo json_encode(findWayPoints($lookup, 'f', 'h')); echo "\n---\n"; echo json_encode(findWayPoints($lookup, 'a', 'c')); echo "\n---\n"; echo json_encode(findWayPoints($lookup, 'a', 'b')); echo "\n---\n"; echo json_encode(findWayPoints($lookup, 'a', 'h')); echo "\n---\n"; echo json_encode(findWayPoints($lookup, 'i', 'j'));
Output for git.master_jit, git.master, rfc.property-hooks
["i","g"] --- ["g","i"] --- ["b","d"] --- [] --- ["b","d","f","g","i"] --- null

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:
41.39 ms | 981 KiB | 4 Q