3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $myArray = array("100_1", "100_2", "100_3", "100_11", "101_1", "102_1", "102_2", "103_1", "103_2", "103_3", "104_1", "104_2"); $resultArray = array(); foreach($myArray as $entry) { $parts = explode("_", $entry); $found = FALSE; foreach($resultArray as $key => $resultEntry) { $resultParts = explode("_", $resultEntry); if($parts[0] == $resultParts[0]) { $found = TRUE; if((int)$parts[1] > (int)$resultParts[1]) { $resultArray[$key] = $entry; } } } if(!$found) { $resultArray[] = $entry; } } print_r($resultArray);
based on 6PjDN
Output for 4.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
Array ( [0] => 100_11 [1] => 101_1 [2] => 102_2 [3] => 103_3 [4] => 104_2 )