fork download
  1. <?php
  2.  
  3. $posts = array(
  4. "ab77" => array( "id" => "ab77" ),
  5. "b4k7" => array( "id" => "b4k7" ),
  6. "4d55" => array( "id" => "4d55" ),
  7. "13c5" => array( "id" => "13c5" ),
  8. "3aa2" => array( "id" => "3aa2" ),
  9. );
  10. $featured = array(
  11. "13c5" => array( "order" => 1 ),
  12. "3a71" => array( "order" => 2 ),
  13. "4d55" => array( "order" => 3 ),
  14. );
  15.  
  16. uksort( $posts, function ( $a, $b ) use ( $featured ) {
  17. $fa = ( isset( $featured[$a] ) ? $featured[$a]['order'] : INF );
  18. $fb = ( isset( $featured[$b] ) ? $featured[$b]['order'] : INF );
  19. if ( $fa != $fb ) return ( $fa < $fb ? -1 : +1 );
  20. // could add a tie-breaker comparison here
  21. return 0;
  22. } );
  23.  
  24. var_dump( $posts );
Success #stdin #stdout 0.02s 24400KB
stdin
Standard input is empty
stdout
array(5) {
  ["13c5"]=>
  array(1) {
    ["id"]=>
    string(4) "13c5"
  }
  ["4d55"]=>
  array(1) {
    ["id"]=>
    string(4) "4d55"
  }
  ["3aa2"]=>
  array(1) {
    ["id"]=>
    string(4) "3aa2"
  }
  ["b4k7"]=>
  array(1) {
    ["id"]=>
    string(4) "b4k7"
  }
  ["ab77"]=>
  array(1) {
    ["id"]=>
    string(4) "ab77"
  }
}