Doc #55069 [Opn->Bgs]: slice_array() does not always reset()?
| From: | frozenfire@php.net | Date: | Fri, 01 Jul 2011 23:22:48 +0000 |
| Subject: | Doc #55069 [Opn->Bgs]: slice_array() does not always reset()? | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-6793@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=55069&edit=1
ID: 55069
Updated by: frozenfire@php.net
Reported by: ross dot crowley at affirmativedesign dot com
Summary: slice_array() does not always reset()?
-Status: Open
+Status: Bogus
Type: Documentation Problem
Package: *General Issues
Operating System: Mac OS X 10.6.7
PHP Version: 5.3.6
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2011-06-29 13:20:29] 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);
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=55069&edit=1