note 126981 added to function.usort
| From: | mail at bemcalavera dot com | Date: | Mon, 11 Apr 2022 10:09:55 +0000 |
| Subject: | note 126981 added to function.usort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-218144@lists.php.net to get a copy of this message | ||
I'm reporting a strange behavior of usort while trying to sort array (with number index in
string format) by subarray item value (see example):
$example = array(
"1" => array("tit" => "title 1", "val" => 2),
"2" => array("tit" => "title 2", "val" => 10),
"3" => array("tit" => "title 3", "val" => 5)
);
usort( $example, function ($a, $b) {
if ( $a["val"] == $b["val"] ) {
return 0;
}
if ( $a["val"] < $b["val"] ) {
return 1;
}else{
return -1;
}
}
In this case usort will mess up all the values of "val" items (!), and in some cases it
creates a new item (with just the "val" field in the new subarray).
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.135 (proxied: 185.5.245.234)
----
Manual Page -- https://php.net/manual/en/function.usort.php
Edit -- https://main.php.net/note/edit/126981
Del: integrated -- https://main.php.net/note/delete/126981/integrated
Del: useless -- https://main.php.net/note/delete/126981/useless
Del: bad code -- https://main.php.net/note/delete/126981/bad+code
Del: spam -- https://main.php.net/note/delete/126981/spam
Del: non-english -- https://main.php.net/note/delete/126981/non-english
Del: in docs -- https://main.php.net/note/delete/126981/in+docs
Del: other reasons-- https://main.php.net/note/delete/126981
Reject -- https://main.php.net/note/reject/126981
Search -- https://main.php.net/manage/user-notes.php