php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #14917 When using array_merge w/numbers & assoc array the numbers are changed to 0,1..
Submitted: 2002-01-07 16:01 UTC Modified: 2002-04-24 10:50 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: blaine_mcdonnell at yahoo dot com Assigned:
Status: Closed Package: Arrays related
PHP Version: 4.1.1 OS: BSD & Windows
Private report: No CVE-ID: None
 [2002-01-07 16:01 UTC] blaine_mcdonnell at yahoo dot com
When I try to use array_merge an array with an associative array the array is renumbered starting at 0.

Problem on Win2k with 4.1.0 & 4.1.1
Problem on OpenBSD with 4.0.6

Here is a quick sample:

<?
	$TEST=array_merge(array("7"=>array(1=>"TEST",2=>"TEST",4=>"TEST"),"9"=>array(1=>"TEST",2=>"TEST",4=>"TEST")),array("A"=>array(1=>"TEST",2=>"TEST",4=>"TEST"),"B"=>array(1=>"TEST",2=>"TEST",4=>"TEST")));
	foreach ($TEST as $key=>$val)
		{print "$key<BR>";}
?>

Output:
0
1
A
B

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-01-08 07:19 UTC] sander@php.net
Reclassified.
 [2002-02-18 15:23 UTC] blaine_mcdonnell at yahoo dot com
This is the work around I used:

function my_array_merge()
	{
	$DATA_ARRAY=func_get_args();
	foreach ($DATA_ARRAY as $key=>$val)
		{
		foreach (testArray($DATA_ARRAY[$key]) as $key1=>$val1)
			{$TEMP_ARRAY[$key1]=$DATA_ARRAY[$key][$key1];}
		}
	return $TEMP_ARRAY;
	}
 [2002-03-22 17:00 UTC] blaine_mcdonnell at yahoo dot com
Still a problem in 4.1.2
 [2002-04-24 10:50 UTC] andrei@php.net
This happens because of the way PHP treats array keys that are quoted numbers, that is, "7" is really an integer key 7 and those do get renumbered by array_merge() as it's supposed to work this way.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 19 15:01:31 2024 UTC