note 126455 added to function.json-decode
| From: | Anonymous at main dot php dot net | Date: | Sat, 25 Sep 2021 02:44:52 +0000 |
| Subject: | note 126455 added to function.json-decode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-217001@lists.php.net to get a copy of this message | ||
<?php
$array = [0 => "foo", 1 => "bar", 2 => ["baz"]];
$associative = false;
var_dump(
json_decode(
json_encode($array),
$associative
)
);
?>
The above will output
<?php
/*
array (size=3)
0 => string 'foo' (length=3)
1 => string 'bar' (length=3)
2 =>
array (size=1)
0 => string 'baz' (length=3)
*/
?>
Considering <?php $associative = false; ?> one might expect
<?php
/*
object(stdClass)
public '0' => string 'foo' (length=3)
public '1' => string 'bar' (length=3)
public '2' =>
object(stdClass)
public '0' => string 'baz' (length=3)
*/
?>
an stdClass object instead of an array, but this is incorrect.
json_decode will always return an array for a sequentially encoded array, regardless of the ?bool
$associative 2nd argument
Which is inline with the above mentioned docs/spec
> Returns the value encoded in json in appropriate PHP type
Considering that the 1st arg was a sequentially encoded array as a json string
'["foo","bar",["baz"]]'
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.85.242 (proxied: 89.164.218.215)
----
Manual Page -- https://php.net/manual/en/function.json-decode.php
Edit -- https://main.php.net/note/edit/126455
Del: integrated -- https://main.php.net/note/delete/126455/integrated
Del: useless -- https://main.php.net/note/delete/126455/useless
Del: bad code -- https://main.php.net/note/delete/126455/bad+code
Del: spam -- https://main.php.net/note/delete/126455/spam
Del: non-english -- https://main.php.net/note/delete/126455/non-english
Del: in docs -- https://main.php.net/note/delete/126455/in+docs
Del: other reasons-- https://main.php.net/note/delete/126455
Reject -- https://main.php.net/note/reject/126455
Search -- https://main.php.net/manage/user-notes.php