php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52677 Numeric members type casting failure
Submitted: 2010-08-23 14:14 UTC Modified: 2010-08-24 11:12 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: michel dot rugenbrink at firstfind dot nl Assigned:
Status: Duplicate Package: Scripting Engine problem
PHP Version: 5.3SVN-2010-08-23 (SVN) OS: Ubuntu
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: michel dot rugenbrink at firstfind dot nl
New email:
PHP Version: OS:

 

 [2010-08-23 14:14 UTC] michel dot rugenbrink at firstfind dot nl
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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-08-24 11:12 UTC] aharvey@php.net
-Status: Open +Status: Duplicate
 [2010-08-24 11:12 UTC] aharvey@php.net
This was bug #45959, and was deemed won't fix for performance reasons.

For the record, this is documented at http://php.net/array#language.types.array.casting
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC