php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #55723 Array of references return copies of elements instead of references
Submitted: 2011-09-19 02:46 UTC Modified: 2011-09-19 09:48 UTC
From: exsystemchina at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.3.8 OS: Windows XP Pro SP 3
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: exsystemchina at gmail dot com
New email:
PHP Version: OS:

 

 [2011-09-19 02:46 UTC] exsystemchina at gmail dot com
Description:
------------
When trying to get a element of reference inside an array, php always returns a 
copy of that variable being referred. See the comment of Test Script.

Test script:
---------------
<?php
$Item = array ('foo' => 'bar');
$Array = array (&$Item);
$rItem = $Array[0]; //$rItem is expected to be a reference, but it doesn't.
$rItem['foo'] = 'foobar';
echo $Item['foo'];

Expected result:
----------------
foobar

Actual result:
--------------
bar

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-09-19 02:50 UTC] exsystemchina at gmail dot com
[Sorry, Grammar Error.]When trying to get [an] element of reference inside an 
array, php always returns a 
copy of that variable being referred. See the comment of Test Script.
 [2011-09-19 04:58 UTC] laruence@php.net
-Status: Open +Status: Bogus
 [2011-09-19 04:58 UTC] laruence@php.net
see blow:
<?php
$Item = array ('foo' => 'bar');
$Array = array (&$Item);
$rItem = & $Array[0]; 
$rItem['foo'] = 'foobar';
echo $Item['foo'];
 [2011-09-19 07:09 UTC] exsystemchina at gmail dot com
OK. it's wired though.
 [2011-09-19 09:41 UTC] exsystemchina at gmail dot com
Wired, still problemetic.
Compare with those two scripts:
<?php
	$ArrayMeta = array('a'=>1, 'b'=>array ('bb' => 2));
	$mPendingStack = array (&$ArrayMeta); 

	$mCurrArray = null; //Reference to current array.
	while (count($mPendingStack) != 0) {
		$mCurrArray = &$mPendingStack[count($mPendingStack)-1]; 
		unset($mPendingStack[count($mPendingStack)-1]);
		reset($mCurrArray);
		for ($i = 0; $i < count($mCurrArray); ++$i) {
			if (is_array($mCurrArray[key($mCurrArray)])) {
				$mPendingStack[count($mPendingStack)] = &$mCurrArray[key($mCurrArray)];
			}
			else {
				$mCurrArray[key($mCurrArray)] = null;
			}
			next($mCurrArray);
		}
	}
	
	var_dump($ArrayMeta);
?>
and
<?php


	$ArrayMeta = array('a'=>1, 'b'=>array ('bb' => 2));
	$mPendingStack = array (&$ArrayMeta); 

	$mCurrArray = null; //Reference to current array.
	while (count($mPendingStack) != 0) {
		$mCurrArray = &$mPendingStack[count($mPendingStack)-1]; 
		unset($mPendingStack[count($mPendingStack)-1]);
		foreach ($mCurrArray as &$mValue) {
			if (is_array($mValue)) {
				$mPendingStack[count($mPendingStack)] = &$mValue;
			}
			else {
				$mValue = null;
			}
		}
	}

	var_dump($ArrayMeta);
?>
Array elements of references can be found via both two ouputs, though those outputs were different. But the expected result should contains no reference, since I var_dump-
ed $ArrayMeta, which should be no reference inside it.

I think it is little bit buggy.

Those are tested under PHP 5.3.8 and PHP 5.3.5.
 [2011-09-19 09:48 UTC] exsystemchina at gmail dot com
<?php
	$ArrayMeta = array('a'=>1, 'b'=>array ('bb' => 2));
	var_dump($ArrayMeta);
	$mPendingStack = array (&$ArrayMeta); 

	$mCurrArray = null; //Reference to current array.
	while (count($mPendingStack) != 0) {
		$mCurrArray = &$mPendingStack[count($mPendingStack)-1];
		unset($mPendingStack[count($mPendingStack)-1]);
		foreach ($mCurrArray as &$mValue) {
			if (is_array($mValue)) {
				$mPendingStack[count($mPendingStack)] = $mValue; //If I removed & before $mValue...
			}
			else {
				$mValue = null;
			}
		}
	}
	
	var_dump($ArrayMeta);

And still, PHP Manual shows me that the & symbol in foreach clause will produce reference. But this is also problemetic in this script. Only $ArrayMeta['a'] turned to 
null.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 16 23:01:33 2025 UTC