note 125418 added to function.asort
| From: | marty8zhang at gmail dot com | Date: | Tue, 06 Oct 2020 04:26:53 +0000 |
| Subject: | note 125418 added to function.asort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-215534@lists.php.net to get a copy of this message | ||
"This function sorts an array such that array indices maintain their correlation with the array
elements they are associated with."
Note this sentence in the description has a quite important implication especially when you're
working with an originally non-indexed (or say integer-indexed) array and JSON encoding/decoding.
I'm not reporting this as a bug, because the end result does fit into the description.
<?php
$myArray = ['small_image', 'image'];
var_dump(json_encode($myArray)); // string(23)
"["small_image","image"]"
var_dump(is_object($myArray)); // bool(false)
asort($myArray);
$myJson = json_encode($myArray);
var_dump($myJson); // string(31)
"{"1":"image","0":"small_image"}"
var_dump(is_object($myArray)); // bool(false)
/*
* class stdClass#1 (2) {
* public $1 =>
* string(5) "image"
* public $0 =>
* string(11) "small_image"
* }
*/
var_dump(json_decode($myJson));
?>
----
Server IP: 45.112.84.4
Probable Submitter: 101.0.109.162 (proxied: 13.54.132.42)
----
Manual Page -- http://php.net/manual/en/function.asort.php
Edit -- https://master.php.net/note/edit/125418
Del: integrated -- https://master.php.net/note/delete/125418/integrated
Del: useless -- https://master.php.net/note/delete/125418/useless
Del: bad code -- https://master.php.net/note/delete/125418/bad+code
Del: spam -- https://master.php.net/note/delete/125418/spam
Del: non-english -- https://master.php.net/note/delete/125418/non-english
Del: in docs -- https://master.php.net/note/delete/125418/in+docs
Del: other reasons-- https://master.php.net/note/delete/125418
Reject -- https://master.php.net/note/reject/125418
Search -- https://master.php.net/manage/user-notes.php