php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75611 array_slice BC breaking
Submitted: 2017-12-01 15:22 UTC Modified: 2017-12-01 15:31 UTC
From: nodex at icloud dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 7.1.12 OS: MacOs and Ubuntu
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
36 - 8 = ?
Subscribe to this entry?

 
 [2017-12-01 15:22 UTC] nodex at icloud dot com
Description:
------------
When using array_slice on a smaller array, PHP 7.1.4 (untested newer versions) returns a blank array if the size of the array is less than the $length parameter of array_slice

In php 5.6 this was not the case, the function simply returned the entire array if it was shorter.

Surely this must be a bug or are we now meant to count the array to check it's the right size before calling array_slice?

Test script:
---------------
# Basic reproduction test
$array = range(0,10);
$slice = array_slice($array,12); // same with and without the offset parameter!
print_r($slice); // Empty!

$array = range(0.5);
$slice = array_slice($array,3);
print_r($slice); // As expected 3 elements!


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-12-01 15:25 UTC] peehaa@php.net
Always been like that: https://3v4l.org/tPsel
 [2017-12-01 15:29 UTC] peehaa@php.net
Also is in the docs: 

> If the offset is larger than the size of the array then returns an empty array. 

Also it seems like you are confusing the length (3rd param) with the offset (2nd param)
 [2017-12-01 15:30 UTC] peehaa@php.net
-Status: Open +Status: Not a bug
 [2017-12-01 15:30 UTC] peehaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php
 [2017-12-01 15:31 UTC] nodex at icloud dot com
Hi. Thanks for your comment.

In php 5.6 it returns the entire array.

I don't think you have understaood what I meant so I'm sorry for that.

I am not sure how to explain it in a better way?
 [2017-12-01 15:37 UTC] peehaa@php.net
As you can see on 3v4l your repro script has the exact same output from PHP 4.2 all the way to 7.2: https://3v4l.org/tPsel
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 22:01:28 2024 UTC