Suppose I have an array that I want to sort, and the order is not something computable, but rather, given by another array which lists the keys in the desired order:
$ordering_array = [
'c',
'a',
'k',
'e',
];
As an added complication, not all of these keys might be present in my array to sort:
$given_array = [
'a' => 'value',
'c' => 'value',
'e' => 'value',
];
What is the cleanest / quickest way to achieve this?
My method is:
$new_array = array_fill_keys($ordering_array, NULL);
foreach ($given_array as $key => $value) {
$new_array[$key] = $value;
}
$new_array = array_filter($new_array);
Is there a better way?