Bug #78780 [Opn->Csd]: array_slice ignores last array element with the custom integer indexes
| From: | bogdan dot hmarnii at gmail dot com | Date: | Mon, 04 Nov 2019 14:36:23 +0000 |
| Subject: | Bug #78780 [Opn->Csd]: array_slice ignores last array element with the custom integer indexes | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-223580@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=78780&edit=1
ID: 78780
User updated by: bogdan dot hmarnii at gmail dot com
Reported by: bogdan dot hmarnii at gmail dot com
Summary: array_slice ignores last array element with the
custom integer indexes
-Status: Open
+Status: Closed
Type: Bug
Package: Arrays related
Operating System: Ubuntu 18.04 LTS
PHP Version: 7.3.11
Block user comment: N
Private report: N
New Comment:
I read more properly the documentation and found next tip: 'The offset parameter denotes the
position in the array, not the key.'. However it is a bit tricky.
Previous Comments:
------------------------------------------------------------------------
[2019-11-04 14:31:04] bogdan dot hmarnii at gmail dot com
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 this bug report at https://bugs.php.net/bug.php?id=78780&edit=1