Possible Duplicate:
How do I sort a multidimensional array in php

How can I sort this array by the value of the "order" key? Even though the values are currently sequential, they will not always be.

Array
(
    [0] => Array
        (
            [hashtag] => a7e87329b5eab8578f4f1098a152d6f4
            [title] => Flower
            [order] => 3
        )

    [1] => Array
        (
            [hashtag] => b24ce0cd392a5b0b8dedc66c25213594
            [title] => Free
            [order] => 2
        )

    [2] => Array
        (
            [hashtag] => e7d31fc0602fb2ede144d18cdffd816b
            [title] => Ready
            [order] => 1
        )
)
share|improve this question
feedback

marked as duplicate by casperOne Jul 17 '12 at 12:10

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5 Answers

up vote 72 down vote accepted
function aasort (&$array, $key) {
    $sorter=array();
    $ret=array();
    reset($array);
    foreach ($array as $ii => $va) {
        $sorter[$ii]=$va[$key];
    }
    asort($sorter);
    foreach ($sorter as $ii => $va) {
        $ret[$ii]=$array[$ii];
    }
    $array=$ret;
}

aasort($your_array,"order");
share|improve this answer
1  
Take a look at en.wikipedia.org/wiki/Sorting_algorithm for sorting algoritms to be able to sort your data as efficient as possible – Robert Apr 23 '10 at 13:59
10  
This could be inefficient, not (only) because of the algorithm, but because php is dumb slow on iterations. (and foreach makes - and iterates over - copies of arrays in memory silently) As someone (studer and fabry) already said below, PHP has a number of sort contraptions (usort,uasort,uksort) which take custom cmp callbacks as arguments. – ZJR Apr 23 '10 at 16:42
You really should use an answer below by @Petah instead of using own sorting algorythms unless you know what you do (and can write code for at least 3 different sorting algorythms yourself). – Aldekein Oct 30 '12 at 9:28
feedback

I use this function :

function array_sort_by_column(&$arr, $col, $dir = SORT_ASC) {
    $sort_col = array();
    foreach ($arr as $key=> $row) {
        $sort_col[$key] = $row[$col];
    }

    array_multisort($sort_col, $dir, $arr);
}


array_sort_by_column($array, 'order');
share|improve this answer
5  
+1 Better alternative – diEcho May 20 '11 at 7:39
2  
Yep this is great, works on strings-based columns, Cheers! – Pete Herbert Penito Jun 3 '11 at 16:41
Thanks, works well ! – Fedir Jan 4 at 11:08
feedback

I usually use usort, and pass my own comparison function. In this case, it is very simple:

function compareOrder($a, $b)
{
  return $a['order'] - $b['order'];
}

usort($array, 'compareOrder');
share|improve this answer
3  
Dammit, I was 30 seconds slower. Isn't it $a - $b though? – christian studer Apr 23 '10 at 14:06
1  
I always get this wrong. Let me think from the manual: the return value must be less than zero if the first argument is considered less than the second. So if $a['order'] is 3, and $b['order'] is 6, I will return -3. Correct? – Jan Fabry Apr 23 '10 at 14:22
2  
Well, you return b - a, so it will be 3. And thus sorted incorrectly. – christian studer Apr 23 '10 at 15:10
8  
Ah. OK. I was using non-logical arithmetic, where the idea in your head does not match the words you produce. This is studied most frequently on Friday afternoons. – Jan Fabry Apr 23 '10 at 21:02
feedback

Try a usort: If you don't have access to anonymous functions yet (Before PHP 5.3), you'll have to define a sorting function first:

function sortByOrder($a, $b) {
    return $a['order'] - $b['order'];
}

usort($myArray, 'sortByOrder');

Updated for PHP 5.3:

usort($myArray, function($a, $b) {
    return $a['order'] - $b['order'];
});
share|improve this answer
2  
My phrasing is a little bit off, I'll edit it. What I meant is that if you're not on PHP 5.3, you need to create a special function just for this particular sorting (Which is somehow not very elegant). Otherwise you could use an anonymous function right there. – christian studer Apr 24 '10 at 4:40
7  
Simple and fast - this should be chosen as the correct answer. In case you are within class scope, consider calling your function with usort($myArray,array($this, 'sortByOrder')); – Mateng Jan 21 '12 at 16:01
4  
I've found this answer in several places (including php.net) but I'm just too dumb to get it. I don't understand how it works. How does usort know which values to pass to sortByOrder...and what values are then being compared in the return? I just don't understand how usort works yet. Would you mind trying to explain? – Jonathan Feb 10 '12 at 17:54
2  
@Jonathan: You can't really see most of the work PHP does. It takes the array and starts with two elements, which it passed to this user defined function. Your function is then responsible to compare them: If the first element is bigger than the second, return a positive integer, if it is smaller, return a negative one. If they are equel, return 0. PHP then sends two other elements to your function and continues to do so, until the array has been sorted. The function here is very short, it might be much more complicated if you wouldn't be comparing integers. – christian studer Feb 13 '12 at 10:40
3  
PHP 5.3 version of this is cute – Stephen Sarcsam Kamenar Oct 12 '12 at 19:32
show 1 more comment
feedback
$sort = array();
$array_lowercase = array_map('strtolower', $array_to_be_sorted);
array_multisort($array_lowercase, SORT_ASC, SORT_STRING, $alphabetically_ordered_array);

This takes care of both upper and lower case alphabets.

share|improve this answer
feedback

Not the answer you're looking for? Browse other questions tagged or ask your own question.