Edit report at http://bugs.php.net/bug.php?id=52677&edit=1

 ID:                 52677
 Updated by:         [email protected]
 Reported by:        michel dot rugenbrink at firstfind dot nl
 Summary:            Numeric members type casting failure
-Status:             Open
+Status:             Duplicate
 Type:               Bug
 Package:            Scripting Engine problem
 Operating System:   Ubuntu
 PHP Version:        5.3SVN-2010-08-23 (SVN)
 Block user comment: N

 New Comment:

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


Previous Comments:
------------------------------------------------------------------------
[2010-08-23 14:14:59] 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.


------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=52677&edit=1

Reply via email to