php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #30305 array_splice() discards keys of incoming array (needs 'preserve_keys' parameter)
Submitted: 2004-10-01 18:20 UTC Modified: 2014-04-17 14:37 UTC
Votes:2
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: conciseusa at yahoo dot com Assigned:
Status: Duplicate Package: Arrays related
PHP Version: * OS: Irrelevant
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: conciseusa at yahoo dot com
New email:
PHP Version: OS:

 

 [2004-10-01 18:20 UTC] conciseusa at yahoo dot com
Description:
------------
It seems to be impossible to replace a key in associative array with another associative array : array_splice always add a '0'  or next unused numeric key for incoming array... This would seem to be a bug.

Reproduce code:
---------------
<?php
        $a = array("a" => "AA", "b" => "BB", "c" => "CC");
        $a2 = $a;
        $z = array("z" => array("z1"=>"ZZ1","z2"=>"ZZ2"), "x" =>"XX");
        
// 1 try to replace "b"=>"BB" by contents of $z
        array_splice($a, 1, 1, $z);
        print_r($a);
// produces : Array ( [a] => AA [0] => Array ( [z1] => ZZ1 [z2] => ZZ2 )
[1] => XX [c] => CC ) 
        
// 2 try to replace "b"=>"BB" by contents of $z as in manual
        array_splice($a, 1, 1, array($z));
        print_r($a);
// produces: Array ( [a] => AA [0] => Array ( [z] => Array ( [z1] => ZZ1 [z2] => ZZ2 ) [x] => XX ) [1] => XX [c] => CC )
?>

Expected result:
----------------
// Should be :
// Array ( [a] => AA [z] => Array ( [z1] => ZZ1 [z2] => ZZ2 ) [1] => XX
[c] => CC )


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-04-17 14:37 UTC] levim@php.net
-Status: Open +Status: Duplicate -Package: Feature/Change Request +Package: Arrays related -Operating System: * +Operating System: Irrelevant -PHP Version: 5CVS, 4CVS (2004-09-29) +PHP Version: *
 [2014-04-17 14:37 UTC] levim@php.net
Duplicate of https://bugs.php.net/bug.php?id=11074
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Feb 05 17:01:30 2025 UTC