From:
Operating system: Ubuntu
PHP version: 5.3SVN-2010-08-23 (SVN)
Package: Scripting Engine problem
Bug Type: Bug
Bug description:Numeric members type casting failure
Description:
------------
When typecasting an object (stdClass in the example below) to an array and
the object contains numeric members the result is a valid array as one
might expect.
var_dumping the array shows all the keys and corresponding values just the
way you might expect. However when an attempt is made to access one of the
numeric array keys a notice Undefined <offset|index> is thrown and the key
is inaccessible.
Several workarounds are possible, like json_encoding/decoding and doing an
array_combine(array_keys(array), array_values(array)) fixes the problem as
well.
Interesting is that the array_key_exists shows that the key does not exist
while var_dumping the array_keys shows the key in question.
Also using array_search() on the value of the key returns the key in
question.
This has been tested on:
PHP 5.2.0-8+etch16 (cli) (built: Nov 24 2009 11:14:47)
PHP 5.3.2
Both had the same issue.
Test script:
---------------
<?php
error_reporting(E_ALL);
$test_object = new stdClass();
$test_object->{1234} = "test";
var_dump($test_object); // object(stdClass)#1 (1) { ["1234"]=> string(4)
"test" }
$test_array = (array) $test_object;
var_dump($test_array); // array(1) { ["1234"]=> string(4) "test" }
$value = $test_array[1234]; // Results in "Notice: Undefined offset: 1234"
$value = $test_array["1234"]; // Results in "Undefined index: 1234"
var_dump(array_key_exists("1234", $test_array)); // bool(false)
var_dump(in_array("1234", array_keys($test_array))); // bool(true)
var_dump(array_search("test", $test_array)); // string(4) "1234"
$new_test_array = array_combine(array_keys($test_array),
array_values($test_array));
var_dump(array_key_exists(1234, $new_test_array)); // bool(true)
$new_value = $new_test_array[1234]; // No notices thrown
var_dump($new_value); // string(4) "test"
?>
Expected result:
----------------
Expected would be the full accessibility of all array keys even after type
casting between object and array.
Actual result:
--------------
The actual result is an array, that looks right, but has inaccessible keys.
--
Edit bug report at http://bugs.php.net/bug.php?id=52677&edit=1
--
Try a snapshot (PHP 5.2):
http://bugs.php.net/fix.php?id=52677&r=trysnapshot52
Try a snapshot (PHP 5.3):
http://bugs.php.net/fix.php?id=52677&r=trysnapshot53
Try a snapshot (trunk):
http://bugs.php.net/fix.php?id=52677&r=trysnapshottrunk
Fixed in SVN:
http://bugs.php.net/fix.php?id=52677&r=fixed
Fixed in SVN and need be documented:
http://bugs.php.net/fix.php?id=52677&r=needdocs
Fixed in release:
http://bugs.php.net/fix.php?id=52677&r=alreadyfixed
Need backtrace:
http://bugs.php.net/fix.php?id=52677&r=needtrace
Need Reproduce Script:
http://bugs.php.net/fix.php?id=52677&r=needscript
Try newer version:
http://bugs.php.net/fix.php?id=52677&r=oldversion
Not developer issue:
http://bugs.php.net/fix.php?id=52677&r=support
Expected behavior:
http://bugs.php.net/fix.php?id=52677&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=52677&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=52677&r=submittedtwice
register_globals:
http://bugs.php.net/fix.php?id=52677&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=52677&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=52677&r=dst
IIS Stability:
http://bugs.php.net/fix.php?id=52677&r=isapi
Install GNU Sed:
http://bugs.php.net/fix.php?id=52677&r=gnused
Floating point limitations:
http://bugs.php.net/fix.php?id=52677&r=float
No Zend Extensions:
http://bugs.php.net/fix.php?id=52677&r=nozend
MySQL Configuration Error:
http://bugs.php.net/fix.php?id=52677&r=mysqlcfg