php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #42941 array_slice resets the current array position
Submitted: 2007-10-12 08:47 UTC Modified: 2008-02-19 01:01 UTC
From: jonas at skubic dot se Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 5CVS-2007-10-12 (snap) OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: jonas at skubic dot se
New email:
PHP Version: OS:

 

 [2007-10-12 08:47 UTC] jonas at skubic dot se
Description:
------------
array_slice seems to be resetting the current array position *before* processing the offset parameter.

This makes it impossible to directly use expressions that use the array position for the offset parameter.

It would be mildly useful and less surprising if the parameters were evaluated before altering the array.

Reproduce code:
---------------
$a = range(0, 9);
next($a);
next($a);

$b = array_slice($a, key($a));

print_r($b);

Expected result:
----------------
Array
(
    [0] => 2
    [1] => 3
    [2] => 4
    [3] => 5
    [4] => 6
    [5] => 7
    [6] => 8
    [7] => 9
)


Actual result:
--------------
Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
    [5] => 5
    [6] => 6
    [7] => 7
    [8] => 8
    [9] => 9
)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-10-13 20:48 UTC] felipensp at gmail dot com
PHP 5.2.5-dev (cli) (built: Oct 13 2007 17:24:05) 

felipe@bl4ck:~/public_html$ php test.php
Array
(
    [0] => 2
    [1] => 3
    [2] => 4
    [3] => 5
    [4] => 6
    [5] => 7
    [6] => 8
    [7] => 9
)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 12 20:01:33 2024 UTC