3v4l.org

run code in 300+ PHP versions simultaneously
<?php $sort_me = [ ["file" => "Desert.jpg"], ["file" => "What.jpg"], ["file" => "Hello.jpg"], ["file" => "Test.jpg"], ["file" => "Goodness.jpg"], ]; $sort_order = ["Test.jpg", "Hello.jpg", "NotFound.jpg"]; $lookup = array_flip($sort_order); $fallback = count($sort_order); usort($sort_me, function($a, $b) use ($lookup, $fallback) { return [$lookup[$a['file']] ?? $fallback, $a['file']] <=> [$lookup[$b['file']] ?? $fallback, $b['file']]; }); var_export($sort_me);
Output for 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.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.27, 8.4.1 - 8.4.14
array ( 0 => array ( 'file' => 'Test.jpg', ), 1 => array ( 'file' => 'Hello.jpg', ), 2 => array ( 'file' => 'Desert.jpg', ), 3 => array ( 'file' => 'Goodness.jpg', ), 4 => array ( 'file' => 'What.jpg', ), )

preferences:
185.81 ms | 408 KiB | 5 Q