| Newsgroups |
php.notes |
| Message-ID |
<[email protected]> |
Be aware with array_multisort() and array_column() when you don't want to sort all columns. The last arg ...$rest is used to sort too!
<?php
$data = [
['id' => 5, 'name' => 'Marie'],
['id' => 7, 'name' => 'Lisa'],
['id' => 4, 'name' => 'Marie'],
['id' => 3, 'name' => 'Jean'],
['id' => 1, 'name' => 'Marie'],
['id' => 2, 'name' => 'Jean'],
];
array_multisort(
array_column($data, 'name'), SORT_ASC, SORT_REGULAR,
$data,
);
/*
We could guess :
3 Jean
2 Jean
7 Lisa
5 Marie
4 Marie
1 Marie
But the result is :
2 Jean
3 Jean
7 Lisa
1 Marie
4 Marie
5 Marie
... id column has been sorted too!
*/
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.137 (proxied: 2a01:e0a:591:bc70:9c64:815:49b8:b603)
----
Manual Page -- https://php.net/manual/en/function.array-multisort.php
Edit -- https://main.php.net/note/edit/130470
Del: integrated -- https://main.php.net/note/delete/130470/integrated
Del: useless -- https://main.php.net/note/delete/130470/useless
Del: bad code -- https://main.php.net/note/delete/130470/bad+code
Del: spam -- https://main.php.net/note/delete/130470/spam
Del: non-english -- https://main.php.net/note/delete/130470/non-english
Del: in docs -- https://main.php.net/note/delete/130470/in+docs
Del: other reasons-- https://main.php.net/note/delete/130470
Reject -- https://main.php.net/note/reject/130470
Search -- https://main.php.net/manage/user-notes.php