|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2015-12-23 23:50 UTC] ajf@php.net
Description:
------------
If an array is casted to an object, its integer keys become inaccessible integer(!) key properties. They should be normal string key properties instead.
Test script:
---------------
<?php
$arr = [1, 2, 4];
$obj = (object)$arr;
var_dump($obj);
var_dump($obj->{1}, $obj->{2}, $obj->{3});
var_dump($obj->{'1'}, $obj->{'2'}, $obj->{'3'});
?>
Expected result:
----------------
object(stdClass)#1 (3) {
["0"]=>
int(1)
["1"]=>
int(2)
["2"]=>
int(4)
}
int(1)
int(2)
int(4)
int(1)
int(2)
int(4)
Actual result:
--------------
object(stdClass)#1 (3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(4)
}
Notice: Undefined property: stdClass::$1 in - on line 7
Notice: Undefined property: stdClass::$2 in - on line 7
Notice: Undefined property: stdClass::$3 in - on line 7
NULL
NULL
NULL
Notice: Undefined property: stdClass::$1 in - on line 8
Notice: Undefined property: stdClass::$2 in - on line 8
Notice: Undefined property: stdClass::$3 in - on line 8
NULL
NULL
NULL
PatchesPull Requests
Pull requests:
HistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 16:00:02 2025 UTC |
Er, the test script has an off-by-one error. It should be: <?php $arr = [1, 2, 4]; $obj = (object)$arr; var_dump($obj); var_dump($obj->{0}, $obj->{1}, $obj->{2}); var_dump($obj->{'0'}, $obj->{'1'}, $obj->{'2'}); ?>