3v4l.org

run code in 300+ PHP versions simultaneously
<?php function freqCount(array $values, $searchValue): array { $frequencies = computeFrequencies($values, $searchValue, 0); return array_map(static fn($key) => [$key, $frequencies[$key]], array_keys($frequencies)); } function computeFrequencies(array $values, $searchValue, int $depth): array { $frequencies = [$depth => 0]; foreach ($values as $value) { if ($value === $searchValue) { $frequencies[$depth]++; } elseif (is_array($value)) { foreach (computeFrequencies($value, $searchValue, $depth + 1) as $deeperDepth => $frequency) { $frequencies[$deeperDepth] = ($frequencies[$deeperDepth] ?? 0) + $frequency; } } } return $frequencies; } print_r(freqCount([1, 4, 4, [1, 1, [1, 2, 1, 1]]], 1)); print_r(freqCount([1, 5, 5, [5, [1, 2, 1, 1], 5, 5], 5, [5]], 5)); print_r(freqCount([1, [2], 1, [[2]], 1, [[[2]]], 1, [[[[2]]]]], 2));
Output for 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.30, 8.3.0 - 8.3.29, 8.4.1 - 8.4.14, 8.4.16, 8.5.0 - 8.5.1
Array ( [0] => Array ( [0] => 0 [1] => 1 ) [1] => Array ( [0] => 1 [1] => 2 ) [2] => Array ( [0] => 2 [1] => 3 ) ) Array ( [0] => Array ( [0] => 0 [1] => 3 ) [1] => Array ( [0] => 1 [1] => 4 ) [2] => Array ( [0] => 2 [1] => 0 ) ) Array ( [0] => Array ( [0] => 0 [1] => 0 ) [1] => Array ( [0] => 1 [1] => 1 ) [2] => Array ( [0] => 2 [1] => 1 ) [3] => Array ( [0] => 3 [1] => 1 ) [4] => Array ( [0] => 4 [1] => 1 ) )
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.
Output for 7.2.0 - 7.2.34, 7.3.0 - 7.3.33
Parse error: syntax error, unexpected 'fn' (T_STRING), expecting :: (T_PAAMAYIM_NEKUDOTAYIM) in /in/bSa2i on line 7
Process exited with code 255.

preferences:
173.29 ms | 410 KiB | 5 Q