Bug #67169 [Com]: array_splice all elements, then []= gives wrong index

From: Date: Fri, 02 May 2014 10:54:39 +0000
Subject: Bug #67169 [Com]: array_splice all elements, then []= gives wrong index
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-185579@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=67169&edit=1 ID: 67169 Comment by: nikic@php.net Reported by: gabriel at figdice dot org Summary: array_splice all elements, then []= gives wrong index Status: Closed Type: Bug Package: Arrays related Operating System: Debian Wheezy PHP Version: 5.6.0beta1 Assigned To: nikic Block user comment: N Private report: N New Comment: Thanks for testing the beta and catching this! The bug is fixed for beta 3 (beta 2 still has the broken behavior.) Previous Comments: ------------------------------------------------------------------------ [2014-05-02 10:53:25] 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 04:38:36] requinix@php.net Related To: Bug #67176 ------------------------------------------------------------------------ [2014-05-01 14:38:38] 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 ) */ ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=67169&edit=1

« previous php.bugs (#185579) next »