1

I want to sort a PHP array that have multiple array with key values.

Sample Array:

Array
(
[1240830] => Array
    (
        [name] => Jannah
        [order] => 8
    )

[1240831] => Array
    (
        [name] => Eadie
        [order] => 10
    )

[1240832] => Array
    (
        [name] => [email protected]
        [order] => 0
    )

[1240911] => Array
    (
        [name] => on
        [order] => 0
    )

)

So I need array that sorted according to Order value in ASC. How can I achieve this?

2 Answers 2

1

For this you can use usort (https://www.php.net/manual/en/function.usort.php).

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

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

Comments

0

You can use your simple own function for that by specifying which column you want to use for sorting:

    function arr_multisort($arr, $column) {
        $c = array_column($arr, $column);
        array_multisort($c, SORT_ASC, $arr);
        return $arr;
    }

In your case:

$sortedArray = arr_multisort($originalArray, 'order');

Comments

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.