From: postings-php-bug at hans-spath dot de
Operating system: Windows + Linux
PHP version: 5.0.3
PHP Bug Type: Variables related
Bug description: array shows different content with print_r/var_export than
with foreach+echo
Description:
------------
While playing with big arrays I discovered this:
If you assign some array values to a variable by reference, you can fuck
up the array.
When you dump the array contents with print_r() or var_dump() you see
something else that with an combination of foreach and echo (or print_r)
on the value.
If tested my reproduce code with PHP (CLI) 4.3.2, 4.3.8, 4.3.10, 5.0.0 and
5.0.3 under Windows XP SP2. Always the same result. But I discovered the
issue (other code) on a Linux system running PHP (CGI) 4.3.10.
It looks a bit like Bug #29992, and I'm sorry if filing a duplicate.
But I am not complaining about the unexpected values, I am complaining
about getting different content from the same array when using a different
method.
Reproduce code:
---------------
<? $test = array();
for( $i=0; $i<8; $i++ )
$test["key {$i}"] = "value {$i}";
$keys = array( 'key 0', 'key 2', 'key 4' );
foreach( $keys as $key )
$t =& $test[$key];
print_r( $test );
foreach( $test as $k=>$t )
echo "[{$k}] {$t}\n";
echo "---\n";
asort( $test );
foreach( $test as $k=>$t )
echo "[{$k}] {$t}\n";
Expected result:
----------------
[Note: I'm not sure whether it should exactly look like this, but at least
the three blocks should print the same key=>value pairs.]
Array
(
[key 0] => value 0
[key 1] => value 1
[key 2] => value 2
[key 3] => value 3
[key 4] => value 4
[key 5] => value 5
[key 6] => value 6
[key 7] => value 7
)
[key 0] value 0
[key 1] value 1
[key 2] value 2
[key 3] value 3
[key 4] value 4
[key 5] value 5
[key 6] value 6
[key 7] value 7
---
[key 0] value 0
[key 1] value 1
[key 2] value 2
[key 3] value 3
[key 4] value 4
[key 5] value 5
[key 6] value 6
[key 7] value 7
Actual result:
--------------
Array
(
[key 0] => value 0
[key 1] => value 1
[key 2] => value 2
[key 3] => value 3
[key 4] => value 4
[key 5] => value 5
[key 6] => value 6
[key 7] => value 7
)
[key 0] value 0
[key 1] value 1
[key 2] value 2
[key 3] value 3
[key 4] value 3 <--
[key 5] value 5
[key 6] value 6
[key 7] value 7
---
[key 0] value 0
[key 1] value 1
[key 2] value 2
[key 3] value 3
[key 5] value 5
[key 6] value 6
[key 7] value 7 <--
[key 4] value 7 <--
--
Edit bug report at http://bugs.php.net/?id=31615&edit=1
--
Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=31615&r=trysnapshot4
Try a CVS snapshot (php5.0):
http://bugs.php.net/fix.php?id=31615&r=trysnapshot50
Try a CVS snapshot (php5.1):
http://bugs.php.net/fix.php?id=31615&r=trysnapshot51
Fixed in CVS: http://bugs.php.net/fix.php?id=31615&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=31615&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=31615&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=31615&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=31615&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=31615&r=support
Expected behavior: http://bugs.php.net/fix.php?id=31615&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=31615&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=31615&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=31615&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=31615&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=31615&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=31615&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=31615&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=31615&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=31615&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=31615&r=mysqlcfg