0

I have this array

$myArray=array(

'a'=>array('id'=>1,'text'=>'blabla1'),
'b'=>array('id'=>2,'text'=>'blabla2'),
'c'=>array('id'=>3,'text'=>'blabla3'),
'd'=>array('id'=>4,'text'=>'blabla4'),

);

and i want to sort the above array by the keys a,b,c,d, who exist in another array:

$tempArray=array('c','a','d','b');

How can I do that so the $myArray

looks like this:

$myArray=array(

'c'=>array('id'=>3,'text'=>'blabla3'),
'a'=>array('id'=>1,'text'=>'blabla1'),
'd'=>array('id'=>4,'text'=>'blabla4'),
'b'=>array('id'=>2,'text'=>'blabla2'),

);

thanks for helping me!

3
  • Have you tried array_shift()? Commented Mar 8, 2013 at 15:19
  • 3
    php.net/manual/en/function.uksort.php Commented Mar 8, 2013 at 15:20
  • I dont know how array_shift can help, but uksort i think can Commented Mar 8, 2013 at 15:24

1 Answer 1

3

The simplest and likely most efficient way to do this is by iterating the array that holds the sort order and creating a new, sorted array:

$sorted = array();

foreach ($tempArray as $order) {
  if (isset($myArray[$order])) {
    $sorted[$order] = $myArray[$order];
  }
}

print_r($sorted);

This works because associative arrays implicitly have an order of the order in which elements were added to the array.

See it working


EDIT

Any solution involving a sorting function will likely be much less efficient than this. This is because in order to do it you will need to use a function that takes a callback - this already has an implied overhead of the function call.

The sorting functions also work by comparing items, meaning that the complexity any of those solutions will be greater than that of this solution (the complexity of this is simply O(n)). Also, in order to derive the return value for the sorting function you would need to inspect the target array, finding the position of each of the keys being compared, for each comparison, adding even more complexity.

Sign up to request clarification or add additional context in comments.

5 Comments

@MikeB uksort is what he needs
@jBaron You should write it in your question ;-)
@jBaron Any solution involving a sorting function will likely be much less efficient than this. This is because in order to do it you will need to use a function that takes a callback - this already has an implied overhead of the function call. The sorting functions also work by comparing objects, meaning that the complexity any of those solutions will be greater than that of this solution (O(n)). Also, in order to derive the return value for the sorting function you would need to inspect the target array for each comparison, adding even more complexity.
For thousand entries you suggest not using uksort?
@jBaron The more entries you have, the less efficient the sorting functions become, because of the way Quicksort works. This will perform no more operations than the number of elements in $tempArray, whereas the sort functions will perform many more operations for more than just a few elements. Compare this (4 items) with this (5 items). You can see that by adding one more item to the target array, it (probably) adds considerably more than one iteration. The only time it doesn't is when the elements are already in the right order.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.