Skip to content

Instantly share code, notes, and snippets.

@jimboobrien
Forked from wpscholar/array-order-by-keys.php
Created September 20, 2017 23:19
Show Gist options
  • Save jimboobrien/c8c4b44bd73a9572f07ff0df71fd306e to your computer and use it in GitHub Desktop.
Save jimboobrien/c8c4b44bd73a9572f07ff0df71fd306e to your computer and use it in GitHub Desktop.
Order an array based an ordered set of keys. NOTE: Values that don't have a corresponding key in the array of keys will be dropped.
<?php
/**
* Order an array based an ordered set of keys.
* NOTE: Values that don't have a corresponding key in the array of keys will be dropped.
*
* @param array $array
* @param array $keys
* @return array
*/
public static function array_order_by_keys( array $array, array $keys ) {
$ordered = array();
foreach ( $keys as $key ) {
$ordered[$key] = $array[$key];
}
return $ordered;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment