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'); 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.40.0120.00318.71
8.3.30.0110.00319.09
8.3.20.0070.00020.25
8.3.10.0030.00621.95
8.3.00.0040.00419.38
8.2.170.0120.00322.96
8.2.160.0100.00322.31
8.2.150.0070.00024.18
8.2.140.0080.00024.66
8.2.130.0020.00526.16
8.2.120.0050.00518.16
8.2.110.0090.00020.51
8.2.100.0080.00317.72
8.2.90.0040.00418.03
8.2.80.0000.00819.11
8.2.70.0040.00417.50
8.2.60.0080.00018.05
8.2.50.0000.00918.07
8.2.40.0000.00718.16
8.2.30.0000.00818.19
8.2.20.0080.00017.77
8.2.10.0050.00318.02
8.2.00.0000.00717.63
8.1.270.0060.00320.47
8.1.260.0050.00226.35
8.1.250.0070.00028.09
8.1.240.0050.00522.55
8.1.230.0060.00620.80
8.1.220.0000.00917.79
8.1.210.0000.00818.77
8.1.200.0080.00417.25
8.1.190.0000.00917.23
8.1.180.0030.00618.10
8.1.170.0040.00420.27
8.1.160.0070.00022.02
8.1.150.0050.00318.79
8.1.140.0060.00317.38
8.1.130.0030.00317.71
8.1.120.0000.00817.44
8.1.110.0050.00317.40
8.1.100.0080.00017.33
8.1.90.0040.00417.52
8.1.80.0000.00817.49
8.1.70.0050.00217.41
8.1.60.0030.00617.56
8.1.50.0000.00817.45
8.1.40.0040.00417.58
8.1.30.0060.00317.70
8.1.20.0000.00717.57
8.1.10.0000.00717.36
8.1.00.0030.00617.45
8.0.300.0000.00720.15
8.0.290.0100.00016.75
8.0.280.0000.00718.41
8.0.270.0000.00817.33
8.0.260.0050.00317.22
8.0.250.0000.00716.93
8.0.240.0040.00416.97
8.0.230.0000.00716.84
8.0.220.0030.00316.87
8.0.210.0040.00416.93
8.0.200.0030.00316.86
8.0.190.0070.00016.96
8.0.180.0050.00316.93
8.0.170.0040.00416.94
8.0.160.0040.00416.86
8.0.150.0000.00716.91
8.0.140.0000.00716.84
8.0.130.0050.00013.42
8.0.120.0030.00516.77
8.0.110.0040.00416.77
8.0.100.0050.00316.95
8.0.90.0070.00016.72
8.0.80.0070.01116.89
8.0.70.0050.00316.70
8.0.60.0040.00416.78
8.0.50.0040.00416.75
8.0.30.0110.01317.08
8.0.20.0140.00817.40
8.0.10.0050.00216.98
8.0.00.0130.00816.77
7.4.330.0000.00515.09
7.4.320.0000.00616.61
7.4.300.0000.00616.66
7.4.290.0040.00416.60
7.4.280.0060.00316.41
7.4.270.0050.00216.61
7.4.260.0040.00416.45
7.4.250.0050.00216.45
7.4.240.0070.00216.61
7.4.230.0030.00316.63
7.4.220.0120.00616.52
7.4.210.0060.01316.71
7.4.200.0000.00716.42
7.4.160.0130.00316.36
7.4.150.0130.00417.40
7.4.140.0100.00817.86
7.4.130.0110.00516.50
7.4.120.0040.01316.48
7.4.110.0060.01616.60
7.4.100.0090.01316.35
7.4.90.0030.01316.54
7.4.80.0180.00616.49
7.4.70.0150.00916.36
7.4.60.0110.00616.65
7.4.50.0000.00816.20
7.4.40.0060.01216.55
7.4.30.0030.01816.57
7.4.00.0060.00914.65
7.3.330.0020.00213.30
7.3.320.0060.00613.44
7.3.310.0040.00416.27
7.3.300.0040.00216.25
7.3.290.0070.00916.36
7.3.280.0080.00916.37
7.3.270.0130.00317.40
7.3.260.0030.01416.55
7.3.250.0110.00916.56
7.3.240.0100.00716.38
7.3.230.0130.00916.48
7.3.210.0070.01016.32
7.3.200.0060.01519.39
7.3.190.0030.01316.64
7.3.180.0120.00316.60
7.3.170.0080.01216.39
7.3.160.0100.00716.40
7.3.00.0080.00416.57
7.2.330.0130.00516.41
7.2.320.0070.01116.91
7.2.310.0080.00816.43
7.2.300.0080.01416.69
7.2.290.0140.00316.55
7.2.130.0040.01116.88
7.2.120.0070.01016.71
7.2.110.0030.01316.95
7.2.100.0060.00617.03
7.2.90.0000.01416.80
7.2.80.0080.00416.89
7.2.70.0050.00616.69
7.2.60.0050.01016.78
7.2.50.0080.00416.94
7.2.40.0030.00716.69
7.2.30.0070.00717.02
7.2.20.0080.00416.68
7.2.10.0080.00816.90
7.2.00.0020.01018.23
7.1.250.0100.00315.79
7.1.240.0030.00715.84
7.1.230.0030.00915.85
7.1.220.0060.00615.75
7.1.210.0000.00915.84
7.1.200.0020.01015.85
7.1.190.0080.00015.94
7.1.180.0000.01115.52
7.1.170.0040.00915.82
7.1.160.0000.01115.80
7.1.150.0030.01015.94
7.1.140.0090.00315.70
7.1.130.0040.00715.82
7.1.120.0060.00615.81
7.1.110.0060.00615.65
7.1.100.0090.00616.70
7.1.90.0070.00315.68
7.1.80.0070.00315.85
7.1.70.0040.00716.23
7.1.60.0070.01017.68
7.1.50.0050.00516.52
7.1.40.0030.00915.58
7.1.30.0090.00315.39
7.1.20.0000.01215.82
7.1.10.0110.00015.63
7.1.00.0080.03519.12
7.0.330.0080.00315.43
7.0.320.0080.00515.21
7.0.310.0040.00715.02
7.0.300.0040.01215.09
7.0.290.0000.01114.96
7.0.280.0100.00015.57
7.0.270.0060.00615.48
7.0.260.0050.00515.40
7.0.250.0040.01115.57
7.0.240.0090.00315.30
7.0.230.0000.00915.19
7.0.220.0060.00615.55
7.0.210.0060.00915.40
7.0.200.0060.00615.04
7.0.190.0110.00015.29
7.0.180.0040.01115.52
7.0.170.0040.01115.46
7.0.160.0030.00715.36
7.0.150.0050.00515.30
7.0.140.0040.03318.74
7.0.130.0040.01115.52
7.0.120.0000.00915.34
7.0.110.0040.00715.28
7.0.100.0000.01015.51
7.0.90.0040.00715.43
7.0.80.0000.01115.44
7.0.70.0080.00315.28
7.0.60.0110.04317.84
7.0.50.0110.03716.59
7.0.40.0020.02916.51
7.0.30.0180.02316.80
7.0.20.0170.04016.85
7.0.10.0100.03116.83
7.0.00.0040.03416.65
5.6.380.0040.00714.44
5.6.370.0050.00514.53
5.6.360.0070.00314.57
5.6.350.0080.00314.57
5.6.340.0020.00914.43
5.6.330.0060.00614.17
5.6.320.0060.00614.09
5.6.310.0000.01014.61
5.6.300.0060.00614.02
5.6.290.0040.00714.64
5.6.280.0050.03917.61
5.6.270.0030.00714.63
5.6.260.0100.00714.64
5.6.250.0000.01114.65
5.6.240.0030.01014.47
5.6.230.0000.01014.18
5.6.220.0030.00814.14
5.6.210.0020.03817.57
5.6.200.0100.03816.29
5.6.190.0050.04717.43
5.6.180.0080.03417.35
5.6.170.0180.04117.34
5.6.160.0040.04617.56
5.6.150.0030.02716.44
5.6.140.0100.04016.17
5.6.130.0030.03816.27
5.6.120.0030.02417.67
5.6.110.0090.02317.62
5.6.100.0080.03917.69
5.6.90.0070.04717.41
5.6.80.0100.04217.36
5.6.70.0120.02317.41
5.6.60.0100.00314.40
5.6.50.0090.00314.36
5.6.40.0030.00914.45
5.6.30.0080.00414.07
5.6.20.0030.00914.59
5.6.10.0060.00614.05
5.6.00.0040.00414.32
5.5.380.0050.00311.19
5.5.370.0040.00411.11
5.5.360.0030.00611.39
5.5.350.0070.03015.70
5.5.340.0050.03514.67
5.5.330.0060.02015.89
5.5.320.0090.02015.64
5.5.310.0200.03815.74
5.5.300.0050.04314.39
5.5.290.0030.04614.51
5.5.280.0010.03616.04
5.5.270.0080.04216.07
5.5.260.0100.03815.79
5.5.250.0130.03915.84
5.5.240.0020.03115.78
5.5.230.0000.00911.05
5.5.220.0000.00910.98
5.5.210.0060.00011.31
5.5.200.0000.00911.30
5.5.190.0000.00611.04
5.5.180.0000.00811.08
5.5.170.0030.00911.28
5.5.160.0040.00710.93
5.5.150.0040.00411.03
5.5.140.0070.00310.91
5.5.130.0030.00711.21
5.5.120.0000.01110.80
5.5.110.0000.00611.38
5.5.100.0030.00311.42
5.5.90.0030.00310.95
5.5.80.0030.00311.23
5.5.70.0040.00711.23
5.5.60.0000.01311.25
5.5.50.0000.01111.24
5.5.40.0040.00411.19
5.5.30.0030.00610.82
5.5.20.0000.00810.74
5.5.10.0050.00510.93
5.5.00.0030.00311.25
5.4.450.0350.03315.16
5.4.440.0470.03215.38
5.4.430.0070.02815.18
5.4.420.0470.02415.33
5.4.410.0080.03014.95
5.4.400.0230.02814.94
5.4.390.0160.03314.85
5.4.380.0200.03014.84
5.4.370.0150.03314.78
5.4.360.0200.02914.77
5.4.350.0220.02915.04
5.4.340.0180.03314.84
5.4.330.0030.00710.97
5.4.320.0240.02814.86
5.4.310.0250.02614.86
5.4.300.0250.03014.64
5.4.290.0210.02514.89
5.4.280.0180.02714.94
5.4.270.0180.03114.93
5.4.260.0300.02614.68
5.4.250.0190.04314.85
5.4.240.0200.02915.02
5.4.230.0250.02814.73
5.4.220.0150.03114.92
5.4.210.0130.03414.64
5.4.200.0250.02513.75
5.4.190.0200.02714.87
5.4.180.0200.03114.85
5.4.170.0170.02914.82
5.4.160.0200.02714.72
5.4.150.0180.02814.67
5.4.140.0170.03013.58
5.4.130.0180.02713.66
5.4.120.0180.03013.66
5.4.110.0250.02013.60
5.4.100.0130.03513.81
5.4.90.0160.03013.57
5.4.80.0260.03013.76
5.4.70.0200.03013.74
5.4.60.0170.03213.72
5.4.50.0310.02713.83
5.4.40.0180.03513.59
5.4.30.0230.03013.68
5.4.20.0170.03013.86
5.4.10.0180.03013.53
5.4.00.0180.03113.51
5.3.290.0230.02912.57
5.3.280.0240.03212.42
5.3.270.0230.02912.58
5.3.260.0160.03212.50
5.3.250.0240.03112.54
5.3.240.0260.02912.43
5.3.230.0220.04212.50
5.3.220.0250.03512.45
5.3.210.0200.04012.44
5.3.200.0200.03512.49
5.3.190.0230.02312.53
5.3.180.0310.03712.54
5.3.170.0200.04212.42
5.3.160.0290.03312.51
5.3.150.0290.03512.65
5.3.140.0310.03112.46
5.3.130.0180.03312.51
5.3.120.0280.03912.54
5.3.110.0150.03512.53
5.3.100.0210.03912.22
5.3.90.0230.03812.30
5.3.80.0200.03412.17
5.3.70.0330.03012.24
5.3.60.0210.03812.14
5.3.50.0300.02812.15
5.3.40.0160.02912.16
5.3.30.0200.02812.14
5.3.20.0230.02711.99
5.3.10.0230.02512.06
5.3.00.0220.02512.00
5.2.170.0120.02510.76
5.2.160.0140.02410.71
5.2.150.0120.03210.81
5.2.140.0180.01810.72
5.2.130.0170.02210.67
5.2.120.0130.03010.73
5.2.110.0170.03010.71
5.2.100.0220.02810.79
5.2.90.0270.02510.74
5.2.80.0200.02510.71
5.2.70.0120.02310.68
5.2.60.0180.02310.72
5.2.50.0160.02610.65
5.2.40.0100.02510.67
5.2.30.0100.02610.67
5.2.20.0160.02110.63
5.2.10.0150.02510.58
5.2.00.0160.02210.51
5.1.60.0130.01610.13
5.1.50.0160.02510.13
5.1.40.0150.02610.13
5.1.30.0100.02110.36
5.1.20.0170.01910.31
5.1.10.0200.02110.17
5.1.00.0110.02810.22
5.0.50.0080.0169.46
5.0.40.0090.0159.39
5.0.30.0050.0319.30
5.0.20.0030.0229.25
5.0.10.0090.0159.26
5.0.00.0070.0259.22
4.4.90.0150.0138.09
4.4.80.0050.0158.06
4.4.70.0070.0148.12
4.4.60.0070.0128.09
4.4.50.0070.0178.09
4.4.40.0070.0228.09
4.4.30.0050.0178.07
4.4.20.0130.0108.09
4.4.10.0080.0168.10
4.4.00.0080.0218.09
4.3.110.0120.0128.09
4.3.100.0080.0108.09
4.3.90.0050.0158.02
4.3.80.0130.0168.06
4.3.70.0070.0178.01
4.3.60.0100.0108.02
4.3.50.0070.0158.04
4.3.40.0050.0228.04
4.3.30.0030.0147.56
4.3.20.0030.0157.56
4.3.10.0060.0127.56
4.3.00.0050.0148.93

preferences:
49.25 ms | 400 KiB | 5 Q