1

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?

hakre
  • 193,403
  • 52
  • 435
  • 836
  • 3
    http://php.net/ksort - if that's not the function you're looking for, scroll down for a list of related functions or look on the left side for *all* array functions. – hakre Jun 27 '12 at 22:04
  • Um hey closvoter, how is a generic question on array ordering _too localized_? If you want it closed, search and find a duplicate, as there is likely to be one. But don't cheat with a bogus close reason. – Michael Berkowski Jun 27 '12 at 22:07
  • @Michael: Smells like a dupe I'd say. Therefore too localized, the problem has been asked and solved already, it's too localized to ask the question again. – hakre Jun 27 '12 at 22:22
  • @hakre Not by my reading of the _too localized_ definition, not even a little. I agree it, it smells like a dupe to me as well, and it's therefore on the community to find it and link it appropriately. – Michael Berkowski Jun 27 '12 at 22:24
  • @Michael: That's probably more correct, yes. – hakre Jun 27 '12 at 22:28

3 Answers3

3

You can use uksort to sort your array (by keys) based on another array (this will work in PHP 5.3+ only):

$pets = array(
   'cat' => 'Lushy',
   'dog' => 'Fido',
   'fish' => 'Goldie' 
);
$sort = array(
    'fish',
    'dog',
    'cat'
);
uksort($pets, function($a, $b) use($sort){
    $a = array_search($a, $sort);
    $b = array_search($b, $sort);

    return $a - $b;
});

DEMO: http://codepad.viper-7.com/DCDjik

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • @ExplosionPills: How would `ksort` know what order to put them in? – gen_Eric Jun 27 '12 at 22:08
  • 1
    I thought `ksort` would sort strings alphabetically (or `krsort` in this case) but maybe it doesn't. That's what I'm asking. I haven't actually checked myself. – Explosion Pills Jun 27 '12 at 22:30
  • @ExplosionPills: It will, but I figured that he wanted a custom sort order, and that it was (reverse) alphabetical by coincidence. – gen_Eric Jun 27 '12 at 22:32
2

You already have the order, so you only need to assign values (Demo):

$sorted = array_merge(array_flip($order), $pets);

print_r($sorted);

Output:

Array
(
    [fish] => Goldie
    [dog] => Fido
    [cat] => Lushy
)

Related: Sort an array based on another array?

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
0

What you need is uksort.

// callback 
function pets_sort($a,$b) {
    // compare input vars and return less than, equal to , or greater than 0. 
} 

uksort($pets, "pets_sort");
Jeshurun
  • 22,940
  • 6
  • 79
  • 92