php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #13478 array_merge changes keys
Submitted: 2001-09-28 08:19 UTC Modified: 2001-09-28 08:28 UTC
From: nick at macaw dot demon dot co dot uk Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 4.0.6 OS:
Private report: No CVE-ID: None
 [2001-09-28 08:19 UTC] nick at macaw dot demon dot co dot uk
Found in 4.0.7RC2 but probably present before. Array merge changes keys that are string-numeric. e.g.

<?
	$arr1 = array('1'=>'one', '2'=>'two');
	$arr2 = array('a'=>'all');

	print_r(array_merge($arr1, $arr2));
?>

gives:

Array ( [0] => one [1] => two [a] => all ) 

A workaround kludge is to prefix string-numeric keys with a letter. 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-09-28 08:28 UTC] derick@php.net
This is not a bug, but a feature. All string indexes are converted to numbers if possible, even like this:
<?php
    $a = array ('4' => "test");
    print_r ($a);
?>

will show:

Array
(
    [4] => test
)

Making it bogus
 [2003-11-25 07:30 UTC] partage_php at yahoo dot fr
<?

It's a bug !
Look this :

<?
$tbl_1 = array("025" => "hello 025",
               "050" => "hello 050",
               "120" => "hello 120");
			   
$tbl_2 = array("010" => "hello 010",
               "130" => "hello 130");

?>

Gives :

Array ( [025] => hello 025 [050] => hello 050 [0] => hello 120 [010] => hello 010 [1] => hello 130 ) 

Why '[1] => hello 130' and not '[130] => hello 130' ?
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Tue Mar 26 16:01:26 2019 UTC