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
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: 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

Pull Requests

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: Sat Dec 21 18:01:29 2024 UTC