php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45346 object to array cast indexing error
Submitted: 2008-06-24 10:30 UTC Modified: 2008-10-25 13:07 UTC
Votes:3
Avg. Score:4.0 ± 0.8
Reproduced:3 of 3 (100.0%)
Same Version:3 (100.0%)
Same OS:0 (0.0%)
From: xavier dot pinard at laposte dot net Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.2.6 OS: WIN XP
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: xavier dot pinard at laposte dot net
New email:
PHP Version: OS:

 

 [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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-09-12 22:50 UTC] steven at acko dot net
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.)
 [2008-09-30 13:05 UTC] xavier dot pinard at laposte dot net
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
 [2008-10-25 13:07 UTC] jani@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php


 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Feb 20 12:01:28 2025 UTC