php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #55069 array_slice() does not always reset()?
Submitted: 2011-06-29 13:20 UTC Modified: 2011-07-01 20:45 UTC
From: ross dot crowley at affirmativedesign dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.3.6 OS: Mac OS X 10.6.7
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: ross dot crowley at affirmativedesign dot com
New email:
PHP Version: OS:

 

 [2011-06-29 13:20 UTC] ross dot crowley at affirmativedesign dot com
Description:
------------
---
From manual page: http://www.php.net/function.array-slice%23Description
---
array_slice() should reset() the array pointer; however, I've observed the 
following:

// MySQL formatted date
$string = '2011-06-30 02:49:24';

// Regex to convert date to parts, with time optional
preg_match('/^(\d{4})-(\d{2})-(\d{2})( (\d{2}):(\d{2}):(\d{2})\.?\d?)?$/', 
$string, $parts);

$output = array_slice($parts, 0, 4) + array_slice($parts, 5);

print_r($output);

Basically, this was just a quick, dirty way to get date/time values into parts, 
but ignore the match of just the time ($parts[4]). I expected to get:

Array
(
    [0] => 2011-06-30 02:49:24
    [1] => 2011
    [2] => 06
    [3] => 30
    [4] => 02
    [5] => 49
    [6] => 24
)

But I always got:

Array
(
    [0] => 2011-06-29 21:49:24
    [1] => 2011
    [2] => 06
    [3] => 29
)

However, this does produce the expected result:

$array = array_slice($parts, 0, 4) + array_slice($parts, 1);
print_r($array);

Note that the second slice starts the slice at index 1 and not 5 as I'd expect 
if index() had been reset by first call to array_slice.

Apparently, the reset() is called only after completion of the full statement 
and not by the function itself? Not sure if this is a bug or an undocumented 
feature.

My example is a bit silly, I know, as I should have array_spliced() in this 
case, but the output 

Test script:
---------------
// MySQL formatted date
$string = '2011-06-30 02:49:24';

// Regex to convert date to parts, with time optional
preg_match('/^(\d{4})-(\d{2})-(\d{2})( (\d{2}):(\d{2}):(\d{2})\.?\d?)?$/', $string, $parts);

$output = array_slice($parts, 0, 4) + array_slice($parts, 5);

print_r($output);


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-07-01 19:22 UTC] frozenfire@php.net
-Status: Open +Status: Bogus
 [2011-07-01 19:22 UTC] frozenfire@php.net
It seems that you're adding arrays together with the addition operator (+). This 
is not how array concatenation works in PHP. You need to use array_merge to merge 
two arrays into one.

Addition of two arrays with the addition operator seems to simply return the first 
array.
 [2011-07-01 20:06 UTC] niel at catweasel dot org
The + symbol for arrays is the union operator, see http://php.net/manual/en/language.operators.array.php for details. Simply put, it adds NEW keys from the right array and ignores existing ones.
 [2011-07-01 20:45 UTC] felipe@php.net
-Summary: slice_array() does not always reset()? +Summary: array_slice() does not always reset()?
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Thu Oct 08 02:00:01 2026 UTC