Bug #53838 [Sus->Csd]: Double Indexes in arrays / objects after cast

From: Date: Mon, 14 Nov 2016 18:29:31 +0000
Subject: Bug #53838 [Sus->Csd]: Double Indexes in arrays / objects after cast
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-205366@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=53838&edit=1 ID: 53838 Updated by: ajf@php.net Reported by: bobwei9 at hotmail dot com Summary: Double Indexes in arrays / objects after cast -Status: Suspended +Status: Closed Type: Bug Package: Class/Object related Operating System: Mac OS X 10.6 PHP Version: 5.3.5 -Assigned To: +Assigned To: ajf Block user comment: N Private report: N New Comment: 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 Previous Comments: ------------------------------------------------------------------------ [2016-08-08 12:17:57] cmb@php.net Related To: Bug #71208 ------------------------------------------------------------------------ [2011-01-25 22:49:22] 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? ------------------------------------------------------------------------ [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. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=53838 -- Edit this bug report at https://bugs.php.net/bug.php?id=53838&edit=1

« previous php.bugs (#205366) next »