php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #31615 array shows different content with print_r/var_export than with foreach+echo
Submitted: 2005-01-19 20:03 UTC Modified: 2005-03-10 19:10 UTC
From: postings-php-bug at hans-spath dot de Assigned:
Status: Not a bug Package: Variables related
PHP Version: * OS: *
Private report: No CVE-ID: None
 [2005-01-19 20:03 UTC] postings-php-bug at hans-spath dot de
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   <--

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-01-26 04:33 UTC] sniper@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip


 [2005-01-28 22:07 UTC] postings-php-bug at hans-spath dot de
Problem still exists.

D:\PHP>5-lastest\php.exe -v
PHP 5.1.0-dev (cli) (built: Jan 28 2005 20:18:19)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v2.1.0-dev, Copyright (c) 1998-2004 Zend Technologies

D:\PHP>5-lastest\php.exe -f test\array_references_foreach.php
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   <--

D:\PHP>4-lastest\php.exe -v
PHP 4.3.11-dev (cgi-fcgi) (built: Jan 28 2005 14:16:59)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies

D:\PHP>4-lastest\php.exe -f test\array_references_foreach.php
[... same result as above ...]
 [2005-03-10 03:16 UTC] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

This is what you did.

The line:
foreach( $keys as $key )
	$t =& $test[$key];

make sthe last accessed key's value referenced by $t

so no change here and print_r() works as you expected.

now comes:
foreach( =>$t)
that means you are reusing a variable that is already a reference to key4. So whenforeach is at three it sets key4's value to 3. Thus you see 3. But still the looping assigns and so the last foreach looping does the last assign which is 7 which is why the third block shows 7.
 [2005-03-10 19:10 UTC] postings-php-bug at hans-spath dot de
Oh I see. I'm sorry.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 14:01:29 2024 UTC