3v4l.org

run code in 300+ PHP versions simultaneously
<?php $order = array( 3452342, 5867867, 7867867, 1231233 ); $array = array( array("id" => 7867867, "title" => "Must Be #3"), array("id" => 3452342, "title" => "Must Be #1"), array("id" => 1231233, "title" => "Must Be #4"), array("id" => 5867867, "title" => "Must Be #2"), array("id" => 1111111, "title" => "Dont Care #1"), array("id" => 2222222, "title" => "Dont Care #2"), array("id" => 3333333, "title" => "Dont Care #3"), array("id" => 4444444, "title" => "Dont Care #4") ); function custom_compare($a, $b){ global $order; $a = array_search($a["id"], $order); echo $a + "\r\n"; $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 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Fatal error: Uncaught TypeError: Unsupported operand types: int + string in /in/t9tgk:22 Stack trace: #0 [internal function]: custom_compare(2, Array) #1 /in/t9tgk(37): usort(Array, 'custom_compare') #2 {main} thrown in /in/t9tgk on line 22
Process exited with code 255.
Output for 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
Warning: A non-numeric value encountered in /in/t9tgk on line 22 2 Warning: A non-numeric value encountered in /in/t9tgk on line 22 2 Warning: A non-numeric value encountered in /in/t9tgk on line 22 3 Warning: A non-numeric value encountered in /in/t9tgk on line 22 2 Warning: A non-numeric value encountered in /in/t9tgk on line 22 0 Warning: A non-numeric value encountered in /in/t9tgk on line 22 3 Warning: A non-numeric value encountered in /in/t9tgk on line 22 0 Warning: A non-numeric value encountered in /in/t9tgk on line 22 0 Warning: A non-numeric value encountered in /in/t9tgk on line 22 03452342Must Be #1 5867867Must Be #2 7867867Must Be #3 1231233Must Be #4 1111111Dont Care #1 2222222Dont Care #2 3333333Dont Care #3 4444444Dont Care #4
Output for 7.0.0 - 7.0.31
2232030003452342Must Be #1 5867867Must Be #2 7867867Must Be #3 1231233Must Be #4 1111111Dont Care #1 2222222Dont Care #2 3333333Dont Care #3 4444444Dont Care #4
Output for 4.3.0 - 4.3.11, 4.4.0 - 4.4.9, 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.38
1100002300000203030003452342Must Be #1 5867867Must Be #2 7867867Must Be #3 1231233Must Be #4 4444444Dont Care #4 3333333Dont Care #3 1111111Dont Care #1 2222222Dont Care #2

preferences:
237.42 ms | 404 KiB | 421 Q