3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $order = array( 85, 35, 58, 68 ); $array = array( array("id" => 58, "title" => "Must Be #3"), array("id" => 85, "title" => "Must Be #1"), array("id" => 68, "title" => "Must Be #4"), array("id" => 35, "title" => "Must Be #2"), array("id" => 69, "title" => "Dont Care #1"), array("id" => 77, "title" => "Dont Care #2"), array("id" => 79, "title" => "Dont Care #3"), array("id" => 80, "title" => "Dont Care #4") ); function custom_compare($a, $b){ global $order; $a = array_search($a["id"], $order); $b = array_search($b["id"], $order); if($a === false && $b === false) { // both items are dont cares return 0; // a == b } else if ($a === false) { // $a is a dont care item return 1; // $a > $b } else if ($b === false) { // $b is a dont care item return -1; // $a < $b } else { return $a - $b; } } usort($array, "custom_compare"); foreach($array as $item): echo $item["id"]; echo $item["title"]; echo "\r\n"; endforeach; ?>
Output for 7.0.0 - 7.1.0
85Must Be #1 35Must Be #2 58Must Be #3 68Must Be #4 69Dont Care #1 77Dont Care #2 79Dont Care #3 80Dont Care #4
Output for 4.3.0 - 5.6.28
85Must Be #1 35Must Be #2 58Must Be #3 68Must Be #4 80Dont Care #4 79Dont Care #3 69Dont Care #1 77Dont Care #2