php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #67169 array_splice all elements, then []= gives wrong index
Submitted: 2014-05-01 14:38 UTC Modified: 2014-05-01 20:54 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: gabriel at figdice dot org Assigned: nikic (profile)
Status: Closed Package: Arrays related
PHP Version: 5.6.0beta1 OS: Debian Wheezy
Private report: No CVE-ID: None
 [2014-05-01 14:38 UTC] gabriel at figdice dot org
Description:
------------
REGRESSION in beta1: the scenario worked fine until 5.6.0alpha1 incl.


When splicing off all the elements of an indexed array,
thus leaving the array empty,
a subsequent all to the shortcut []= assignment will set the new item
at a key which is not zero (unlike expected) but the previous "next index",
which is what would happen should we not have spliced the array empty.

For example, if you append two items in an array, then array_splice them off (from index 0, for length 2, replacing with nothing), then if later on you append a brand new element in your emptied array, it gets index 2 instead of expected 0.


Test script:
---------------
<?php

// Bug in PHP 5.6.0-beta-1
// also failing in PHP 5.7.0-dev

// When splicing off all the elements of an indexed array,
// thus leaving the array empty,
// a subsequent all to the shortcut []= assignment will set the new item
// at a key which is not zero (unlike expected) but the previous "next index",
// which is what would happen should we not have spliced the array empty.

$arr = array();
$arr[] = 'something';
$arr[] = 'some more';
array_splice($arr, 0, 2);

$arr[] = 'should be index zero';
print_r($arr);

/* will produce:

Array
(
    [2] => should be index zero
)

*/



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-05-01 20:54 UTC] nikic@php.net
-Assigned To: +Assigned To: nikic
 [2014-05-02 10:53 UTC] nikic@php.net
Automatic comment on behalf of nikic
Revision: http://git.php.net/?p=php-src.git;a=commit;h=69b5ee61d029b5532cd47ccae6ab0546b6d53106
Log: Fixed bug #67169: []= after_array_splice incorrect
 [2014-05-02 10:53 UTC] nikic@php.net
-Status: Assigned +Status: Closed
 [2014-05-02 10:54 UTC] nikic@php.net
Thanks for testing the beta and catching this! The bug is fixed for beta 3 (beta 2 still has the broken behavior.)
 [2014-05-05 06:56 UTC] ab@php.net
Automatic comment on behalf of nikic
Revision: http://git.php.net/?p=php-src.git;a=commit;h=69b5ee61d029b5532cd47ccae6ab0546b6d53106
Log: Fixed bug #67169: []= after_array_splice incorrect
 [2014-05-05 07:00 UTC] ab@php.net
Automatic comment on behalf of nikic
Revision: http://git.php.net/?p=php-src.git;a=commit;h=69b5ee61d029b5532cd47ccae6ab0546b6d53106
Log: Fixed bug #67169: []= after_array_splice incorrect
 [2014-05-14 07:57 UTC] tyrael@php.net
Automatic comment on behalf of nikic
Revision: http://git.php.net/?p=php-src.git;a=commit;h=69b5ee61d029b5532cd47ccae6ab0546b6d53106
Log: Fixed bug #67169: []= after_array_splice incorrect
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 10:01:30 2024 UTC