I have this array:
$pets = array(
'cat' => 'Lushy',
'dog' => 'Fido',
'fish' => 'Goldie'
);
If I need to reorder the arrays by having:
fish
dog
cat
in that order and assuming that any of those values may or may not be present, is there a better way than:
$new_ordered_pets = array();
if(isset($pets['fish'])) {
$new_ordered_pets['fish'] = $pets['fish'];
}
if(isset($pets['dog'])) {
$new_ordered_pets['dog'] = $pets['dog'];
}
if(isset($pets['cat'])) {
$new_ordered_pets['cat'] = $pets['cat'];
}
var_dump($new_ordered_pets);
outputs:
Array
(
[fish] => Goldie
[dog] => Fido
[cat] => Lushy
)
Is there a cleaner way, perhaps some inbuilt function I'm not aware of that you simply supply the array to be reordered and the indexes you would like it to be recorded by and it does the magic?