8
[
    1 => ['id' => 1, 'sort' => 1],
    3 => ['id' => 3, 'sort' => 3],
    2 => ['id' => 2, 'sort' => 2],
]

How do I sort it so that it's re-ordered using the inner 'sort' key? Desired result:

[
    1 => ['id' => 1, 'sort' => 1],
    2 => ['id' => 2, 'sort' => 2],
    3 => ['id' => 3, 'sort' => 3],
]
3
  • Major amounts of duplicates: stackoverflow.com/search?q=php+sort (Damn, why is this such a hot topic lately?) Commented Sep 27, 2010 at 13:39
  • Generally PHP.net has a lot of really useful notes from users, did you look through the sort page, and if so, where did things go wrong? It's best if we can help you if we know what you have tried and what issued you're having. php.net/manual/en/function.sort.php Commented Sep 27, 2010 at 13:52
  • @dennis It's September 27th, I think the first round of school projects are due. Commented Sep 27, 2010 at 13:52

4 Answers 4

19

You can use usort with this comparison function:

function cmpBySort($a, $b) {
    return $a['sort'] - $b['sort'];
}
usort($arr, 'cmpBySort');

Or you use array_multisort with an additional array of key values for the sort order:

$keys = array_map(function($val) { return $val['sort']; }, $arr);
array_multisort($keys, $arr);

Here array_map with the anonymous function is used to build an array of the sort values that is used to sort the array values itself. The advantage of this is that there is np comparison function that needs to be called for each pair of values.

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

2 Comments

array_multisort worked for me as well. I changed $val['sort'] into ($val['sort'] * -1) to sort by descending
Both snippets will destroy the original numeric keys. 3v4l.org/2Gn1U gogagubi -- if you want descending sorting, use the SORT_DESC flag as the parameter between the two parameters of array_multisort() in Gumbo's answer.
4

Something like this:

usort($array, function (array $a, array $b) { return $a["sort"] - $b["sort"]; });

1 Comment

Please delete this duplicate (and code-only) answer. I will not serve to benefit SO readers.
0

Something like this:

uasort($array, 'compfunc');

function compfunc($a, $b)
{
    return $a['sort'] - $b['sort'];
}

Comments

0

Implement the latest syntax (since PHP7.4) use array function syntax with the spaceship operator. Writing $a on the left and $b on the right gives an ASC sort. Conversely $b <=> $a gives a DESC sort.

uasort() preserves the original first level keys (which usort() will not do and array_multisort() will not do with numeric keys).

Code: (Demo)

$array = [
    1 => ['id' => 1, 'sort' => 1],
    3 => ['id' => 3, 'sort' => 3],
    2 => ['id' => 2, 'sort' => 2],
];

uasort($array, fn($a, $b) => $a['sort'] <=> $b['sort']);
var_export($array);

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.