3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ '1' => true, ['2' => false, '4' => true], '3' => true ]; function findKeysWithTrueValues(array $array): array { $matches = []; foreach ($array as $key => $value) { if (is_array($value)) { foreach (findKeysWithTrueValues($value) as $match) { $matches[] = $match; } } elseif ($value === true) { $matches[] = $key; } } return $matches; } print_r(findKeysWithTrueValues($array));

preferences:
26.13 ms | 404 KiB | 5 Q