php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64005 array_merge memorry issues.
Submitted: 2013-01-16 10:10 UTC Modified: 2013-01-18 00:22 UTC
From: youri dot essa at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.4.10 OS: Linux
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: youri dot essa at gmail dot com
New email:
PHP Version: OS:

 

 [2013-01-16 10:10 UTC] 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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-01-18 00:22 UTC] googleguy@php.net
-Status: Open +Status: Not a bug
 [2013-01-18 00:22 UTC] googleguy@php.net
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()."
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 01 21:01:35 2025 UTC