php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #11074 array_splice used to insert associative array forgets first key
Submitted: 2001-05-23 22:04 UTC Modified: 2005-03-04 12:01 UTC
From: symetrie at online dot fr Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 4.3.10 OS:
Private report: No CVE-ID: None
 [2001-05-23 22:04 UTC] symetrie at online dot fr
It seems to be impossible to replace a key in associative array with another array : array_splice always add a '0' key... Feature or bug ?
<?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 ) 
        
        echo "<hr>";
        
        // 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 ) 
// I'd like :
// Array ( [a] => AA [z] => Array ( [z1] => ZZ1 [z2] => ZZ2 ) [1] => XX [c] => CC ) 
?>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-02-26 21:37 UTC] yohgaki@php.net
The version of PHP that this bug was reported in is too old. Please
try to reproduce this bug in the latest version of PHP (available
from http://www.php.net/downloads.php

If you are still able to reproduce the bug with one of the latest
versions of PHP, please change the PHP version on this bug report
to the version you tested and change the status back to "Open".


 [2005-03-04 12:01 UTC] techtonik@php.net
array_splice doesn't preserve keys from replacement arrays.
I wonder if it is possible to add optional "preserve_keys" parameter in PHP5?
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Thu Jun 20 07:01:25 2019 UTC