note 130470 added to function.array-multisort
| From: | seb@example.net | Date: | Tue, 02 Sep 2025 17:32:36 +0000 |
| Subject: | note 130470 added to function.array-multisort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-222330@lists.php.net to get a copy of this message | ||
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