Bug #53838 [Sus]: Double Indexes in arrays / objects after cast
| From: | bobwei9 at hotmail dot com | Date: | Tue, 25 Jan 2011 21:49:22 +0000 |
| Subject: | Bug #53838 [Sus]: Double Indexes in arrays / objects after cast | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-158179@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=53838&edit=1
ID: 53838
User updated by: bobwei9 at hotmail dot com
Reported by: bobwei9 at hotmail dot com
Summary: Double Indexes in arrays / objects after cast
Status: Suspended
Type: Bug
Package: Class/Object related
Operating System: Mac OS X 10.6
PHP Version: 5.3.5
Block user comment: N
Private report: N
New Comment:
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?
Previous Comments:
------------------------------------------------------------------------
[2011-01-25 22:39:31] 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 21:17:07] 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 21:11:06] 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 20:33:14] 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 20:31:17] 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
)
)
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=53838&edit=1