I want to be able to sort an array in a specific key order.
Suppose the following array:
$titles = array(
'United States' => 'Dibbuk Box',
'Australia' => 'The Possession',
'UK' => 'The Possession',
'getTitle' => 'The Possession',
);
The preference array (where the sorting order of the keys will be determined from) will be something like:
$preference = array(
'UK',
'USA',
'World-wide, English title',
'United Kingdom',
'United Kingdom, English title',
'United States',
'United States, English title',
'World-wide',
'Australia',
'getTitle',
);
So after the sorting takes place, I would like the first array to become like this:
$titles = array(
'UK' => The Possession',
'United States' => 'Dibbuk Box',
'Australia' => 'The Possession',
'getTitle' => 'The Possession',
)
So I had the impression that the code below would work, but it doesn't...
array_multisort($preference, SORT_STRING, $titles);
What am I doing wrong?
Actually, what I'm trying to achieve is to retrieve a movie title according to an order or preference. So I'd like to get the UK title. If not available, I'd like to get the USA title. If that's not available, I'd like to take the World-wide, English title, etc...
I know I can do it with nested if/else's. I just thought of the sorted array solution as it will be more elegant and short. So, I'll first sort the array in the order of preference, and then I'll take the 1st value.
If anyone has a better solution in mind, feel free to elaborate. TIA
I believe I know what your getting at and here is a function I've been using for some time that allows sorting associative/multi-dimensional arrays:
Sample using this on a single associative array: