How to sort a PHP array based on a second array? -


i have 2 simple arrays. 1 array of posts ($posts) , other array of post id's should on top of stream, featured ($featured). trying find efficient way loop through them , if id in both arrays, move beginning of $posts array, , ones moved should sorted asc $featured['order'], example, posts ($posts):

array (     [ab77] => status_update object         (             [id] => ab77             [title] => status update          )      [b4k7] => status_update object          (              [id] => b4k7              [title] => status update          )     [4d55] => status_update object          (              [id] => 4d55              [title] => status update          )      [13c5] => status_update object          (              [id] => 13c5              [title] => status update           )       [3aa2] => status_update object           (               [id] => 3aa2               [title] => status update            ) ) 

and posts should featured ($featured):

array (     [13c5] => array          (              [id] => 13c5              [order] => 1          )      [3a71] => array          (              [id] => 3a71              [order] => 2          )      [4d55] => array          (              [id] => 4d55              [order] => 3          )  ) 

so $posts array should end sorted this:

13c5 4d55 ab77 bk47 3aa2

how do without bunch of slow loops?

this should doable uksort() , closures:

uksort( $posts, function ( $a, $b ) use ( $featured ) {     $fa = ( isset( $featured[$a] ) ? $featured[$a]['order'] : inf );     $fb = ( isset( $featured[$b] ) ? $featured[$b]['order'] : inf );     if ( $fa != $fb ) return ( $fa < $fb ? -1 : +1 );     // add tie-breaker comparison here     return 0; } );