php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53838 Double Indexes in arrays / objects after cast
Submitted: 2011-01-25 20:31 UTC Modified: 2016-11-14 18:29 UTC
From: bobwei9 at hotmail dot com Assigned: ajf (profile)
Status: Closed Package: Class/Object related
PHP Version: 5.3.5 OS: Mac OS X 10.6
Private report: No CVE-ID: None
 [2011-01-25 20:31 UTC] bobwei9 at hotmail dot com
Description:
------------
You can see here that once it's a string-index and once an int-index. But I don't suppose that this result is wanted?
p.ex.: http://bobweinand.no-ip.org/php_test.php?simu%5Bang%5D%5B1%5D=79&simu%5Bver%5D%5B1%5D=&simu%5Bang%5D%5B2%5D=786&simu%5Bver%5D%5B2%5D=&simu%5Bang%5D%5B3%5D=&simu%5Bver%5D%5B3%5D=78786&simu%5Bang%5D%5B4%5D=&simu%5Bver%5D%5B4%5D=8&simu%5Bang%5D%5B5%5D=89&simu%5Bver%5D%5B5%5D=8&simu%5Bang%5D%5B6%5D=&simu%5Bver%5D%5B6%5D=445&simu%5Bang%5D%5B7%5D=&simu%5Bver%5D%5B7%5D=3&simu%5Bang%5D%5B8%5D=&simu%5Bver%5D%5B8%5D=

Test script:
---------------
http://bobweinand.no-ip.org/php_bug_test.php

Expected result:
----------------
string(1) "1"
string(1) "2"
string(1) "3"
string(1) "4"
string(1) "5"
string(1) "6"
string(1) "7"
string(1) "8"
Array
(
    [0] => stdClass Object
        ( 
            [1] => 0
            [2] => 0
            [3] => 0
            [4] => 0
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
        )

    [1] => stdClass Object
        (
            [1] => 
            [2] => 
            [3] => 78786
            [4] => 8
            [5] => 8
            [6] => 445
            [7] => 3
            [8] => 
        )

)


Actual result:
--------------
int(1)
int(2)
int(3)
int(4)
int(5)
int(6)
int(7)
int(8)
string(1) "1"
string(1) "2"
string(1) "3"
string(1) "4"
string(1) "5"
string(1) "6"
string(1) "7"
string(1) "8"
Array
(
    [0] => stdClass Object
        (
            [1] => 79
            [2] => 786
            [3] => 
            [4] => 
            [5] => 89
            [6] => 
            [7] => 
            [8] => 
            [1] => 0
            [2] => 0
            [3] => 0
            [4] => 0
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
        )

    [1] => stdClass Object
        (
            [1] => 
            [2] => 
            [3] => 78786
            [4] => 8
            [5] => 8
            [6] => 445
            [7] => 3
            [8] => 
            [1] => 0
            [2] => 0
            [3] => 0
            [4] => 0
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
        )

)


Patches

Pull Requests

Pull requests:

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-25 20:33 UTC] bobwei9 at hotmail dot com
expected result:

string(1) "1"
string(1) "2"
string(1) "3"
string(1) "4"
string(1) "5"
string(1) "6"
string(1) "7"
string(1) "8"
Array
(
    [0] => stdClass Object
        ( 
            [1] => 0
            [2] => 0
            [3] => 0
            [4] => 0
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
        )

    [1] => stdClass Object
        (
            [1] => 0
            [2] => 0
            [3] => 0
            [4] => 0
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
        )

)
 [2011-01-25 21:11 UTC] rasmus@php.net
-Status: Open +Status: Feedback
 [2011-01-25 21:11 UTC] rasmus@php.net
I don't understand this bug report.  I see this in $_GET:

Array
(
    [ang] => Array
        (
            [1] => 79
            [2] => 786
            [3] => 
            [4] => 
            [5] => 89
            [6] => 
            [7] => 
            [8] => 
        )

    [ver] => Array
        (
            [1] => 
            [2] => 
            [3] => 78786
            [4] => 8
            [5] => 8
            [6] => 445
            [7] => 3
            [8] => 
        )

)

That looks correct to me.
 [2011-01-25 21:17 UTC] bobwei9 at hotmail dot com
-Status: Feedback +Status: Open
 [2011-01-25 21:17 UTC] bobwei9 at hotmail dot com
Yes, but this:

        for ($i=1;8 >= $i;$i++) 
        { 
            $ang->$i *= $ang_fak; 
            $ver->$i *= $ver_fak; 
        } 

creates a new index (string), and don't overwrite the old index (int), and also there are two indexes of the same value (but two different types int and string)
 [2011-01-25 22:39 UTC] rasmus@php.net
-Status: Open +Status: Suspended
 [2011-01-25 22:39 UTC] rasmus@php.net
Right, so your bug report is just a really obfuscated way of reporting this:


$a = array(1=>'abc');
$b = (object)$a;
$i=1;
$b->$i = 'def';
var_dump($b);


which shows the inherent side-effects of casting an array to an object 
indiscriminately.  This is just how it works.  You can kinda-sorta fake numeric 
properties if you try hard, but it is a bad idea.  Fix your code to do:

$a = array('1'=>'abc');
$b = new stdclass;
foreach($a as $k=>$v) $b->$k = $v;
$i=1;
$b->$i = 'def';
var_dump($b);

instead of expecting casting to do the right thing.  Or if you know you are 
going to be using the array as an object, use string indices.  Like a1, a2, a3..
 [2011-01-25 22:49 UTC] bobwei9 at hotmail dot com
But this is very slow. Why don't you can fix type casting from array to object? When int-indexes are always converted to string-indexes this problem will disappear?

Or why are numeric indexes allowed in classes? When numeric index are disabled in classes indexes are always (?) strings?
 [2016-11-14 18:29 UTC] ajf@php.net
-Status: Suspended +Status: Closed -Assigned To: +Assigned To: ajf
 [2016-11-14 18:29 UTC] ajf@php.net
The fix for this bug has been committed.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.

 For Windows:

http://windows.php.net/snapshots/
 
Thank you for the report, and for helping us make PHP better.

Fixed by: https://wiki.php.net/rfc/convert_numeric_keys_in_object_array_casts
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Oct 06 01:01:27 2024 UTC