|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-06-24 10:30 UTC] xavier dot pinard at laposte dot net
Description:
------------
object numeric's name properties are cast in string array's index.
created indexes becomes not accessible.
Reproduce code:
---------------
#5.2.7.-DEV on 2007-06-24
# WANTED => cast object to array and access first element with 0 index
#RESULT => the array key with the index '0' is nor accessible by 0 or "0", uncool ...
#?PROBLEM? => Shouldn't the array key needs to be numeric instead of a string after object to array cast ?
$stdclass = neW StdcLaSs() ;
$stdclass->{0} = 'index0' ;
$stdclassElement = (arRaY)$stdclass ;
$var = array_keys( $stdclassElement ) ;
$key = $var [ 0 ] ;
Echo
'$stdclassElement[ $key ] :: ' , $stdclassElement[ $key ] ,
'$stdclassElement[0] :: ' , $stdclassElement[ 0 ] ,
'$stdclassElement["0"] :: ', $stdclassElement['0']
;
Expected result:
----------------
acess neither to array index '0' by [0] or ["0"]
Actual result:
--------------
the index is not accessible
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Nov 20 13:00:01 2025 UTC |
Here is a minimal test case: <?php $obj = new stdClass(); $obj->{0} = 'foo'; $obj->{'1'} = 'bar'; $obj->{'x'} = 'baz'; $array = (array)$obj; var_dump($array[0]); var_dump($array['1']); var_dump($array['x']); ?> Expected result: --------------- string(3) "foo" string(3) "bar" string(3) "baz" Actual result: -------------- NULL NULL string(3) "baz" If you compare var_dump((array)$obj) as defined above with var_dump(array(0 => 'foo', '1' => 'bar', 'x' => 'baz')), you can see that the numeric array keys are stored as strings rather than integers, which causes the array lookups to fail. (PS: The above developer's response reads like a standard copy/paste reply that ignores the fact that example code was already supplied. If the example was insufficient or unclear, at least take 5 seconds to explain why. The way this bug report is now, it does not help anyone.)Hi, Sorry for embedding huge scripts into the report. I submit you the new test code with open and close tags : <?php $stdclass = neW StdcLaSs() ; $stdclass->{0} = 'index0' ; $stdclassElement = (arRaY)$stdclass ; $var = array_keys( $stdclassElement ) ; $key = $var [ 0 ] ; Echo '$stdclassElement[ $key ] :: ' , $stdclassElement[ $key ] , '$stdclassElement[0] :: ' , $stdclassElement[ 0 ] , '$stdclassElement["0"] :: ', $stdclassElement['0'] ; ?> Hope it's gonna be more useful. Do I have to submit the bug again under a new Id or it's gonna be find like that ? Regards, Xavier