1

I've an array like this

Array
(

[0] => Array

        (
            [result] => Array
                (
                    [0] => Array
                        (
                            [type] => ABC
                            [id] => 123232
                            [name] => Apple

                        )

                    [1] => Array
                        (
                            [type] => DEF
                            [id] => 2323232
                            [name] => Banana

                        )

                )


            [title] => Cool
            [rank] => 2
        )

  [1] => Array
        (
            [result] => Array
                (
                    [0] => Array
                        (
                            [type] => ZX
                            [id] => 3223
                            [name] => Danny

                        )

                    [1] => Array
                        (
                            [type] => QWER
                            [id] => 2323232
                            [name] => Cactus

                        )

                )


            [title] => Hot
            [rank] => 1
        )


[3]..
[4]..and son on

I would like to sort by rank, is there any quick sort method in PHP to do that?

0

3 Answers 3

4

You could use usort().

The example below requires >= PHP 5.3. If you don't have this version, just pass a reference to the callback.

usort($array, function($a, $b) {
   return $a['rank'] - $b['rank'];
}
Sign up to request clarification or add additional context in comments.

Comments

3

You can use the usort function:

function cmp($a, $b) {
    return $a['rank'] - $b['rank'];
}

$arr = /* your array */

usort($arr, "cmp");

See it

To sort on rank in descending order (question asked in comments), you just reverse the order in the compare function:

function cmp($a, $b) {
    return $b['rank'] - $a['rank'];
           ^^           ^^
}

5 Comments

@codaddick; i really doesn't undestand how it works in all sorting methods $a -$b; would you please describe or give me a link to underatand
@diEcho The comparison function only needs to return 0 if both are equal, a value < 0 if the first element is lower and a value > 0 if the first element is higher than the second. Since the ranks are numeric $a - $b accomplishes this.
@diEcho If they are equal, 0 will be returned, if $a is smaller, a negative number will be returned, if $a is bigger, a positive number will be returned.
@diEcho: The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. One can use an else to achieve it but a simpler way is to do return $a - $b;
Thanks all for the super fast reply! wondering how to sort by descending for this case?
0

There are multiple ways to sort an array, see : http://php.net/manual/en/array.sorting.php

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.