php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #8032 array_splice made array keys bad
Submitted: 2000-11-29 10:44 UTC Modified: 2001-08-19 22:56 UTC
From: nimrod at ambernet dot kiev dot ua Assigned:
Status: Closed Package: Arrays related
PHP Version: 4.0.3pl1 OS: FreeBSD 4.2-Release
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: nimrod at ambernet dot kiev dot ua
New email:
PHP Version: OS:

 

 [2000-11-29 10:44 UTC] nimrod at ambernet dot kiev dot ua
<?
$a["e"]="e";
$a["r"]="r";
$a["11"]="111";
$a["22"]="222";

array_splice ($a,0,1);
foreach ($a as $k=>$v) {
    echo "$k=$v<br>";
}
?>

produce:
r=r
0=111
1=222


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-08-19 22:56 UTC] jason@php.net
This is actually not a bug. What you are running in to is php's auto-type conversion when you are building your array.

$a["11"] automagicly will become a numeric index of $a[11]. Due to the way that array_splice is designed, the array is completly changed, and thus the numeric indexes are reset.

If you wish to prevent this from occuring, prefix the numeric string with 0.

ie

<?
           $a["e"]="e";
           $a["r"]="r";
           $a["011"]="111";
           $a["022"]="222";

           array_splice ($a,0,1);
           foreach ($a as $k=>$v) {
               echo "$k=$v<br>";
           }
?>

will produce
r=r
011=111
022=222

-Jason
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed May 29 06:01:32 2024 UTC