3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $items = [ ['name' => 'Hank'], ['name' => 'Beavis'], ['name' => 'Butthead'], ['name' => 'Cornholio', 'caffine_powered' => true], ]; $sorter = function($a, $b){ if( key_exists('caffine_powered', $a) && key_exists('caffine_powered', $b) ) { // both exist return $a['name'] <=> $b['name']; } else if( ! (key_exists('caffine_powered', $a) || key_exists('caffine_powered', $b)) ) { // neither exist return $a['name'] <=> $b['name']; } else if( key_exists('caffine_powered', $a) ) { // just a return -1; } else { // just b return 1; } }; usort($items, $sorter); var_dump($items);
based on 6ORYb
Output for 7.1.25 - 7.3.3
array(4) { [0]=> array(2) { ["name"]=> string(9) "Cornholio" ["caffine_powered"]=> bool(true) } [1]=> array(1) { ["name"]=> string(6) "Beavis" } [2]=> array(1) { ["name"]=> string(8) "Butthead" } [3]=> array(1) { ["name"]=> string(4) "Hank" } }