3v4l.org

run code in 300+ PHP versions simultaneously
<?php $balls = [['001', '001', '003', '004', '005'], ['012', '004', '007', '010', '005'], ['011', '007', '009', '008', '005']]; $searchMatches = function($balls) { foreach ($balls as $rowNumber => $rowData) { $repeating = array_filter(array_count_values($rowData), function ($count) { return $count > 1; }); if ($repeating) { $tmpRow = $rowData; foreach ($repeating as $value => $count) { for ($i = 1; $i <= $count; $i++) { $index = array_search($value, $tmpRow); $matchesRow[$rowNumber][$index] = $value; unset($tmpRow[$index]); } } } } return $matchesRow; }; $reverseBalls = []; foreach($balls as $rowNumber => $rowData) { $count = count($rowData); for($i = 0; $i < $count; $i++) { $reverseBalls[$i][$rowNumber] = $rowData[$i]; } } var_dump($searchMatches($balls)); var_dump($searchMatches($reverseBalls));

preferences:
59.55 ms | 402 KiB | 5 Q