3v4l.org

run code in 300+ PHP versions simultaneously
<?php function findMyThing($needle, $haystack) { // We need to set up a stack array + a while loop to avoid recursive functions for those are evil. // Recursive functions would also complicate things further in regard of returning. $stack = [ [ 'prefix' => [], 'value' => $haystack ] ]; // As long as there's still something there, don't stop while(count($stack) > 0) { // Copy the current stack and create a new, empty one $currentStack = $stack; $stack = []; // Work the stack for($i = 0; $i < count($currentStack); $i++) { // Iterate over the actual array foreach($currentStack[$i]['value'] as $key => $value) { // If the value is an array, then // 1. the key is a string (so we need to match against it) // 2. we might have to go deeper if(is_array($value)) { // We need to build the current prefix list regardless of what we're gonna do below $prefix = $currentStack[$i]['prefix']; $prefix[] = $key; // If the current key, is the one we're looking for, heureka! if($key == $needle) { return $prefix; } // Otherwise, push prefix & value onto the stack for the next loop to pick up else { $stack[] = [ 'prefix' => $prefix, 'value' => $value ]; } } // If the value is NOT an array, then // 1. the key is an integer, so we DO NOT want to match against it // 2. we need to match against the value itself elseif($value == $needle) { // This time append $value, not $key $prefix = $currentStack[$i]['prefix']; $prefix[] = $value; return $prefix; } } } } // At this point we searched the entire array and didn't find anything, so we return an empty array return []; } function getPath($haystack, $needle, $prefix=""){ $path = ""; foreach($haystack as $key=>$value){ if($path!="")break; if($key===$needle){ return $prefix.$key; break; } elseif($value===$needle) { return $prefix.$value; break; } elseif(is_array($value)) { $path.=getPath($value,$needle,$prefix.$key."=>"); } } return $path; } $arr = [ 'USA' => [ 'Alabama' => [ 'Montgomery', 'Birmingham' ], 'Arizona' => [ 'Phoenix', 'Mesa', 'Gilbert' ] ], 'Germany' => [ 'West Germany' => [ 'Bonn', 'Cologne' ] ] ]; echo getPath($arr, 'Alabama')."\n"; echo getPath($arr, 'Cologne');

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.60.0060.01116.38
8.3.50.0120.00918.32
8.3.40.0060.00918.57
8.3.30.0100.00319.01
8.3.20.0070.00020.29
8.3.10.0030.00621.91
8.3.00.0050.00319.38
8.2.180.0170.00318.16
8.2.170.0070.00722.96
8.2.160.0030.01021.98
8.2.150.0080.00024.18
8.2.140.0040.00424.66
8.2.130.0000.00726.16
8.2.120.0090.00319.66
8.2.110.0040.00721.11
8.2.100.0070.00420.39
8.2.90.0060.00319.64
8.2.80.0000.01018.21
8.2.70.0050.00317.93
8.2.60.0000.00817.97
8.2.50.0040.00418.10
8.2.40.0050.00318.05
8.2.30.0030.00518.09
8.2.20.0050.00317.74
8.2.10.0000.00818.02
8.2.00.0040.00417.76
8.1.280.0180.00325.92
8.1.270.0080.00020.79
8.1.260.0080.00026.35
8.1.250.0000.00728.09
8.1.240.0000.00922.19
8.1.230.0090.00319.10
8.1.220.0040.00417.77
8.1.210.0000.00818.77
8.1.200.0060.00317.35
8.1.190.0000.00817.10
8.1.180.0040.00718.10
8.1.170.0080.00020.29
8.1.160.0040.00421.93
8.1.150.0060.00318.96
8.1.140.0040.00417.38
8.1.130.0000.00817.84
8.1.120.0000.00717.51
8.1.110.0040.00417.51
8.1.100.0070.00017.48
8.1.90.0030.00517.43
8.1.80.0000.00717.37
8.1.70.0030.00317.37
8.1.60.0030.00617.59
8.1.50.0040.00417.59
8.1.40.0030.00517.59
8.1.30.0030.00617.68
8.1.20.0040.00417.75
8.1.10.0070.00017.32
8.1.00.0040.00417.55
8.0.300.0040.00419.60
8.0.290.0080.00016.75
8.0.280.0040.00418.50
8.0.270.0070.00017.22
8.0.260.0040.00417.27
8.0.250.0040.00416.85
8.0.240.0000.00716.97
8.0.230.0000.00716.96
8.0.220.0030.00316.78
8.0.210.0040.00416.92
8.0.200.0000.00817.01
8.0.190.0030.00616.95
8.0.180.0050.00216.93
8.0.170.0030.00516.93
8.0.160.0040.00416.95
8.0.150.0040.00416.82
8.0.140.0000.00916.91
8.0.130.0000.00513.37
8.0.120.0000.00816.80
8.0.110.0040.00416.86
8.0.100.0040.00416.79
8.0.90.0040.00416.95
8.0.80.0090.00616.97
8.0.70.0080.00016.79
8.0.60.0040.00416.96
8.0.50.0050.00216.78
8.0.30.0100.01117.01
8.0.20.0150.01017.40
8.0.10.0070.00016.86
8.0.00.0110.00616.92
7.4.330.0020.00215.03
7.4.320.0060.00016.66
7.4.300.0000.00616.63
7.4.290.0000.00716.46
7.4.280.0040.00416.61
7.4.270.0070.00016.38
7.4.260.0040.00416.51
7.4.250.0040.00416.46
7.4.240.0030.00616.52
7.4.230.0030.00316.55
7.4.220.0230.00316.44
7.4.210.0070.01416.51
7.4.200.0000.00716.36
7.4.160.0160.00016.59
7.4.150.0130.00617.40
7.4.140.0080.00917.86
7.4.130.0050.01216.53
7.4.120.0090.00916.51
7.4.110.0090.00916.59
7.4.100.0040.01516.61
7.4.90.0080.01216.46
7.4.80.0030.01716.60
7.4.70.0080.01116.41
7.4.60.0130.01016.48
7.4.50.0050.00316.30
7.4.40.0140.00916.52
7.4.30.0140.01016.72
7.4.00.0070.00714.92
7.3.330.0000.00513.37
7.3.320.0150.00013.36
7.3.310.0040.00416.36
7.3.300.0030.00316.26
7.3.290.0100.00616.37
7.3.280.0110.00516.37
7.3.270.0160.00317.40
7.3.260.0060.01216.40
7.3.250.0070.01316.42
7.3.240.0190.00016.61
7.3.230.0130.00616.64
7.3.210.0110.00516.43
7.3.200.0090.00919.39
7.3.190.0100.00716.65
7.3.180.0090.00616.28
7.3.170.0030.01316.39
7.3.160.0090.00916.61
7.2.330.0070.01116.46
7.2.320.0060.01216.74
7.2.310.0060.01016.57
7.2.300.0000.02016.87
7.2.290.0040.01416.68
7.2.00.0060.00619.52
7.1.100.0060.00618.26
7.1.70.0060.00617.09
7.1.60.0030.02219.50
7.1.50.0070.01516.83
7.1.00.0100.06722.45
7.0.200.0100.00315.05
7.0.140.0030.05322.08
7.0.60.0030.05019.95
7.0.50.0130.08017.87
7.0.40.0000.04720.25
7.0.30.0300.08720.32
7.0.20.0270.08020.15
7.0.10.0200.05720.19
7.0.00.0070.04320.16
5.6.280.0100.06320.88
5.6.210.0070.05720.55
5.6.200.0030.07718.23
5.6.190.0070.08720.45
5.6.180.0330.06020.44
5.6.170.0370.08320.62
5.6.160.0100.04320.39
5.6.150.0070.05018.29
5.6.140.0130.07018.28
5.6.130.0000.09018.15
5.6.120.0130.07021.06
5.6.110.0170.07320.97
5.6.100.0070.07020.99
5.6.90.0170.07721.11
5.6.80.0030.08320.39
5.5.350.0130.05320.50
5.5.340.0100.06317.96
5.5.330.0030.04020.27
5.5.320.0100.05720.27
5.5.310.0200.05020.34
5.5.300.0030.08318.07
5.5.290.0100.07317.98
5.5.280.0130.07720.88
5.5.270.0100.08020.77
5.5.260.0100.06020.89
5.5.250.0000.07720.69
5.5.240.0070.04020.18
5.4.450.0500.06719.47
5.4.440.0930.05719.56
5.4.430.0100.05719.70
5.4.420.0900.05719.46
5.4.410.0770.07019.37
5.4.400.0400.05018.81
5.4.390.0300.06718.80
5.4.380.0500.06318.66
5.4.370.0630.06318.76
5.4.360.0500.06318.64
5.4.350.0470.06718.75
5.4.340.0400.05018.70
5.4.320.0400.06718.72
5.4.310.0430.06318.70
5.4.300.0370.06718.48
5.4.290.0430.06718.74
5.4.280.0300.07318.46
5.4.270.0330.05318.53
5.4.260.0370.06318.61
5.4.250.0430.04318.77
5.4.240.0470.07018.80
5.4.230.0370.05718.57
5.4.220.0430.07018.82
5.4.210.0300.05018.69
5.4.200.0030.06016.64
5.4.190.0230.05318.46
5.4.180.0300.03718.72
5.4.170.0270.06018.52
5.4.160.0370.05718.75
5.4.150.0270.06018.76
5.4.140.0230.06016.46
5.4.130.0400.05316.34
5.4.120.0430.04316.26
5.4.110.0300.06316.23
5.4.100.0230.06716.34
5.4.90.0370.05316.44
5.4.80.0330.05016.18
5.4.70.0300.05316.15
5.4.60.0230.05716.26
5.4.50.0330.05016.42
5.4.40.0400.04716.35
5.4.30.0300.05316.21
5.4.20.0300.05016.32
5.4.10.0230.06016.18
5.4.00.0370.05015.66
5.3.290.0270.07014.79
5.3.280.0270.08314.61
5.3.270.0430.06714.66
5.3.260.0330.07714.63
5.3.250.0370.07014.67
5.3.240.0470.06014.66
5.3.230.0430.05014.65
5.3.220.0370.05014.70
5.3.210.0330.05714.67
5.3.200.0330.05014.59
5.3.190.0430.04014.77
5.3.180.0430.04014.71
5.3.170.0300.05714.77
5.3.160.0370.04714.62
5.3.150.0300.06714.67
5.3.140.0300.05314.74
5.3.130.0330.05714.59
5.3.120.0330.07014.60
5.3.110.0400.07314.73
5.3.100.0330.05314.18
5.3.90.0600.05714.07
5.3.80.0270.06014.01
5.3.70.0330.06014.07
5.3.60.0370.07014.04
5.3.50.0470.06314.07
5.3.40.0430.06014.18
5.3.30.0330.06013.94
5.3.20.0430.04713.70
5.3.10.0430.06013.65
5.3.00.0430.05713.75
5.2.170.0270.04311.35
5.2.160.0270.04311.25
5.2.150.0130.06011.36
5.2.140.0370.03711.18
5.2.130.0200.06711.16
5.2.120.0370.05011.15
5.2.110.0230.04711.16
5.2.100.0200.05011.14
5.2.90.0230.04711.21
5.2.80.0330.03711.21
5.2.70.0270.04311.13
5.2.60.0300.05711.27
5.2.50.0430.05011.04
5.2.40.0170.04711.00
5.2.30.0230.05711.10
5.2.20.0270.04711.03
5.2.10.0300.04710.85
5.2.00.0170.06310.77
5.1.60.0270.04310.20
5.1.50.0200.05010.07
5.1.40.0230.03010.08
5.1.30.0200.03710.44
5.1.20.0230.03710.35
5.1.10.0170.04010.25
5.1.00.0300.03010.15
5.0.50.0200.0278.74
5.0.40.0130.0278.54
5.0.30.0100.0478.41
5.0.20.0170.0278.36
5.0.10.0170.0408.17
5.0.00.0130.0508.23
4.4.90.0130.0206.02
4.4.80.0130.0275.92
4.4.70.0130.0205.90
4.4.60.0130.0206.01
4.4.50.0130.0306.00
4.4.40.0100.0476.00
4.4.30.0200.0205.89
4.4.20.0130.0206.00
4.4.10.0100.0276.02
4.4.00.0130.0336.01
4.3.110.0130.0235.83
4.3.100.0200.0175.80
4.3.90.0170.0275.85
4.3.80.0100.0375.79
4.3.70.0130.0205.91
4.3.60.0100.0205.92
4.3.50.0100.0235.79
4.3.40.0070.0405.79
4.3.30.0030.0274.88
4.3.20.0100.0204.88
4.3.10.0000.0304.88
4.3.00.0170.0237.60

preferences:
52.93 ms | 400 KiB | 5 Q