php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #45024 Its not a bug, its a feature, but a problem.
Submitted: 2008-05-16 20:18 UTC Modified: 2010-12-22 13:49 UTC
Votes:8
Avg. Score:4.6 ± 0.7
Reproduced:7 of 7 (100.0%)
Same Version:3 (42.9%)
Same OS:3 (42.9%)
From: tobias at gaszmann dot de Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.2.6 OS: *
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: tobias at gaszmann dot de
New email:
PHP Version: OS:

 

 [2008-05-16 20:18 UTC] tobias at gaszmann dot de
Description:
------------
Associative Arrays loose their "string-but-numbers-only-keys" when being merged.

array_something["34"]="hello 34";
will get lost when being array_merged.

array_something["34_ID"]="hello 34";
will be ok when being array_merged.

I know this is most likely a feature, but it really did threw me off and took a while to get down to. To me this is an unwanted behavior.

Even more so, as you cannot force the array to remain associative, even not by "(string)" or settype, as you can see in the demo-code. 

Thank you!
Tobias Gassmann


Reproduce code:
---------------
$array_one=array();
$array_one["one"]   = "Database-Entry with UID='one'";
$array_one["2_uid"] = "Database-Entry with UID='2_uid'";
$array_one["ID_3"]  = "Database-Entry with UID='ID_3'";
print_r($array_one);

$array_two=array();
$array_two["four"]  = "Database-Entry with UID='four'";
$array_two["5"]     = "Database-Entry with UID='5'";
$string_six="6";
$array_two["$string_six"] = "Database-Entry with UID='6'";
$string_seven="7";
settype($string_seven,"string");
$array_two["$string_seven"] = "Database-Entry with UID='7'";
$array_two["ID_8"]  = "Database-Entry with UID='ID_8'";
print_r($array_two);

$array_three = array_merge($array_one,$array_two);
print_r($array_three);


Expected result:
----------------
I would have expected for all keys to remain what they are. 

Actual result:
--------------
The keys "5", "6" and "7" got lost, although I declared them as strings.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-12-22 13:49 UTC] johannes@php.net
-Status: Open +Status: Bogus -Package: Feature/Change Request +Package: *General Issues
 [2010-12-22 13:49 UTC] johannes@php.net
numeric string keys are converted to integers automatically to avoid situations where you have $a[1] and $a["1"], this is expected.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Dec 04 11:00:01 2025 UTC