|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2010-08-24 11:12 UTC] aharvey@php.net
-Status: Open
+Status: Duplicate
[2010-08-24 11:12 UTC] aharvey@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 16:00:01 2025 UTC |
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.