Bug #78780 [NEW]: array_slice ignores last array element with the custom integer indexes

From: Date: Mon, 04 Nov 2019 14:31:05 +0000
Subject: Bug #78780 [NEW]: array_slice ignores last array element with the custom integer indexes
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-223575@lists.php.net to get a copy of this message
From: bogdan dot hmarnii at gmail dot com Operating system: Ubuntu 18.04 LTS PHP version: 7.3.11 Package: Arrays related Bug Type: Bug Bug description:array_slice ignores last array element with the custom integer indexes Description: ------------ array_slice() standard function may not work correctly with the Array with the custom integer indexes starting from 1. It is ignoring last element in this type of array. Additionally verified this problem via 3v4l on all php versions, looks like it is existing everywhere. Test script: --------------- <?php // Failing scenario: Custom int keys in array $stack = [ 1 => 'testA', 2 => 'testB', ]; var_dump(array_key_first($stack)); //first key is 1 var_dump(array_slice($stack, 2)); //Possible bug, last element is ignored. Result:[] var_dump(array_slice($stack, 2, null, true)); //preverse_keys = true. Result: [] //Usual working case $stack = ['testA','testB',]; var_dump(array_key_first($stack)); //first key is 0 var_dump(array_slice($stack, 1)); //Last element is not ingored. Result: stack[1]; //Not usual working case $stack = [0 => 'testA', 1 => 'testB',]; //setting customs indexes in the right order var_dump(array_key_first($stack)); //first key is 0 var_dump(array_slice($stack, 1)); //Last element is not ingored. Result: stack[1]; Expected result: ---------------- If we have an array with the custom integer indexes like $array = [1 => '1', 2 => '2']; then array_slice for the last index should return last element in the array. array_slice($array, 2) should return $array[2]. Actual result: -------------- If we have an array with the custom integer indexes like $array = [1 => '1', 2 => '2']; then array_slice for the last index returns empty array. array_slice($array, 2) returns [] (an empty array). -- Edit bug report at https://bugs.php.net/bug.php?id=78780&edit=1 -- Fix committed: https://bugs.php.net/fix.php?id=78780&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=78780&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=78780&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=78780&r=needscript Try newer version: https://bugs.php.net/fix.php?id=78780&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=78780&r=support Expected behavior: https://bugs.php.net/fix.php?id=78780&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=78780&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=78780&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=78780&r=globals PHP version support discontinued: https://bugs.php.net/fix.php?id=78780&r=phptooold Daylight Savings: https://bugs.php.net/fix.php?id=78780&r=dst IIS Stability: https://bugs.php.net/fix.php?id=78780&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=78780&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=78780&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=78780&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=78780&r=mysqlcfg

« previous php.bugs (#223575) next »