1

I have this array :

$order_list = array ( array ("081", "01203", "2", "12000"),
                      array ("002", "01204", "8", NULL),
                      array ("021", "01207", "8", NULL),
                      array ("081", "01206", "8", NULL),
                      array ("043", "01205", "10", "14000"));         

and I want to sort (ascending) that array based on the first value (081, 002, 021, 081 and 043). so it will look like this :

array ( array ("002", "01204", "8", NULL),
        array ("021", "01207", "8", NULL),
        array ("043", "01205", "10", "14000"),
        array ("081", "01203", "2", "12000"),
        array ("081", "01206", "8", NULL));

how to do that? I learned about asort() and ksort(), but it seems that only works for associative array, not multidimensional array.

3
  • 4
    read about usort() Commented Jan 22, 2016 at 10:55
  • 2
    Look over at the Related column on the 'right hand side' of this page... There are a lot of very useful questions and answers. Also, search Google: php sort multi array . Commented Jan 22, 2016 at 11:07
  • 2
    this is such a duplicate question (and any answers) :) It has to one of the most popular questions here. Commented Jan 22, 2016 at 11:10

2 Answers 2

3

You can use usort()

usort($yourArray, function ($a, $b) {
    if ($a[0] == $b[0]) return 0;
    return (int) $a[0] < (int) $b[0] ? -1 : 1;
});

The parameters $a and $b are your (sub)arrays. This simple function compares the first value in each of those.

If you had other than numbers in your array you could have used strcmp():

usort($yourArray, function ($a, $b) {
    return strcmp($a[0], $b[0]);
});
Sign up to request clarification or add additional context in comments.

9 Comments

Be aware that usort exhibits undefined behavior (first Note in that link) if your sorting function returns 0.
@morido, you make it seem worse than it is. It simply states that there is no way of telling if there value 0 is returned then usort cannot tell which of the two (arrays in this case) comes first, which is perfectly fine, since there are deemed to be equal.
I am just saying how it is. After all, it's a beautiful source of (hard to find) bugs. So I prefer to always make the behavior deterministic and either force $a or $b to be sorted in first, rather than to leave this to the implementation.
@morido so what would be the "correct" way to determine -1 or 1 for two arrays that are identical?
Well the point is, that PHP does not make any statements about the order of equal elements. This is different to other languages, where such operations often guarantee stability (i.e. predictable behavior) in such situations. See for instance Arrays.sort in Java.
|
1

It is easier to rewrite this way:

usort($order_list, function($v1, $v2) { return $v1[0] - $v2[0]; });

Or it is very convenient to sort arrays with sorted function from Nspl:

use function \nspl\a\sorted;
use function \nspl\op\itemGetter;

$sortedOrders = sorted($order_list, itemGetter(0));

3 Comments

Nice touch with the return $a - $b
I just tried what would happen if you omitted the cast part (int) that works fine as well. So you can just do return $v1[0] - $v2[0];
Nice finding. Since - is not defined for string PHP is casting values to numeric automatically. Updating the answer.

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.