php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78780 array_slice ignores last array element with the custom integer indexes
Submitted: 2019-11-04 14:31 UTC Modified: 2019-11-04 14:54 UTC
From: bogdan dot hmarnii at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 7.3.11 OS: Ubuntu 18.04 LTS
Private report: No CVE-ID: None
 [2019-11-04 14:31 UTC] 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).

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-11-04 14:36 UTC] bogdan dot hmarnii at gmail dot com
-Status: Open +Status: Closed
 [2019-11-04 14:36 UTC] bogdan dot hmarnii at gmail dot com
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.
 [2019-11-04 14:54 UTC] cmb@php.net
-Status: Closed +Status: Not a bug
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 14:01:30 2024 UTC