Bug #64005 [Opn->Nab]: array_merge memorry issues.

From: Date: Fri, 18 Jan 2013 00:22:18 +0000
Subject: Bug #64005 [Opn->Nab]: array_merge memorry issues.
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-176083@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=64005&edit=1

 ID:                 64005
 Updated by:         googleguy@php.net
 Reported by:        youri dot essa at gmail dot com
 Summary:            array_merge memorry issues.
-Status:             Open
+Status:             Not a bug
 Type:               Bug
 Package:            Arrays related
 Operating System:   Linux
 PHP Version:        5.4.10
 Block user comment: N
 Private report:     N

 New Comment:

This is not a bug, but a simple mistake of using references in foreach. Pelase 
see http://php.net/references and http://php.net/foreach for more details. 
Particularly the red warning box at the bottom of the page at php.net/foreach 
where it states "Reference of a $value and the last array element remain even 
after the foreach loop. It is recommended to destroy it by unset()."


Previous Comments:
------------------------------------------------------------------------
[2013-01-16 10:10:56] youri dot essa at gmail dot com

Description:
------------
---
From manual page: http://www.php.net/function.array-merge#refsect1-function.array-
merge-description
---


Test script:
---------------
foreach($cols as $key => &$value) {
	if(array_key_exists($key, $fields)) {
		$value = array_merge($fields[$key], $value);
	}
}
			
foreach($fields as $key => $value) {
	if(!array_key_exists($key, $cols)) {
		$cols[$key] = $value;
	}
}

Expected result:
----------------
Expected result is that col becomes is an array with all the values that where 
already defined added with the values from fields.

Actual result:
--------------
The sub-array in cols wich was merged with the same sub-array in fields becomes 
overwritten everytime the next foreach statement accesses the value $value, witch 
result. the fix for this is to have a diffrent name for the array merging, this 
leads me to belive that array_merge keeps re-evaluating the values.


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=64005&edit=1


Thread (2 messages)

« previous php.bugs (#176083) next »