3v4l.org

run code in 300+ PHP versions simultaneously
<?php $orderDoc = array("ORT", "TRI", "CONT", "RMI"); $FindDoc= array("RMI0000191","ORT0000379","ORT0000391","ORT0000392","ORT0000390","CONT0000274","CONT0000275","RMI0000192","ORT0000391"); $result =[]; foreach($orderDoc as $doc){ $temp = preg_grep("/^". preg_quote($doc) . "/", $FindDoc); sort($temp); $result = array_merge($result, $temp); } var_dump($result);
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
array(9) { [0]=> string(10) "ORT0000379" [1]=> string(10) "ORT0000390" [2]=> string(10) "ORT0000391" [3]=> string(10) "ORT0000391" [4]=> string(10) "ORT0000392" [5]=> string(11) "CONT0000274" [6]=> string(11) "CONT0000275" [7]=> string(10) "RMI0000191" [8]=> string(10) "RMI0000192" }

preferences:
194.88 ms | 404 KiB | 214 Q