Req #15520 [Opn->Csd]: array_slice destroys numeric keys
| From: | datibbaw@php.net | Date: | Tue, 23 Aug 2011 14:27:36 +0000 |
| Subject: | Req #15520 [Opn->Csd]: array_slice destroys numeric keys | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-162855@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=15520&edit=1
ID: 15520
Updated by: datibbaw@php.net
Reported by: david at acz dot org
Summary: array_slice destroys numeric keys
-Status: Open
+Status: Closed
Type: Feature/Change Request
-Package: Feature/Change Request
+Package: *General Issues
Operating System: Linux
PHP Version: 4.3.0-dev
-Assigned To:
+Assigned To: datibbaw
Block user comment: N
Private report: N
New Comment:
This bug has been fixed in SVN.
Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
For Windows:
http://windows.php.net/snapshots/
Thank you for the report, and for helping us make PHP better.
Fixed in 5.0.2 with the introduction of the fourth 'preserve_keys' parameter.
<?php
$a = array("7" => "foo", "f" => "bar", "13"
=> "blah");
print_r(array_slice($a, 0, count($a), true));
?>
Array
(
[7] => foo
[f] => bar
[13] => blah
)
Previous Comments:
------------------------------------------------------------------------
[2002-07-03 21:16:38] eru@php.net
Got me convinced: re-categorizing, -versioning, etc.
------------------------------------------------------------------------
[2002-07-03 20:29:07] david at acz dot org
This is a bug. Or at least, it is a very serious documentation problem. The function is taking the
slices correctly. But it is destroying numeric keys, and leaving non-numeric keys alone. Why does
it arbitrarily change keys that happen to be numeric? Array keys are not based on numbers, so
array_slice should not be concerned with their actual value, especially if it is using the actual
element positions to calculate the slice.
------------------------------------------------------------------------
[2002-07-03 20:06:40] eru@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
From the manual-page:
Note that array_slice() will ignore array keys, and will calculate offsets and lengths based on the
actual positions of elements within the array.
------------------------------------------------------------------------
[2002-02-12 08:59:34] david at acz dot org
The array_slice function destroys keys that are numeric (even if used as strings), but not string
keys. This is a serious bug, which makes the function unusable. I had to write my own function to
correctly take the slice of an array. If this is indeed intended behavior, then it should be noted
as such in the manual, and a new function added to take array slices that does not destroy numeric
keys.
<?
$a = array("7" => "foo", "f" => "bar",
"13" => "blah");
print_r($a);
print_r(array_slice($a, 0));
?>
Array
(
[7] => foo
[f] => bar
[13] => blah
)
Array
(
[0] => foo
[f] => bar
[1] => blah
)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=15520&edit=1