php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #15520 array_slice destroys numeric keys
Submitted: 2002-02-12 08:59 UTC Modified: 2011-08-23 14:27 UTC
Votes:2
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: david at acz dot org Assigned: datibbaw (profile)
Status: Closed Package: *General Issues
PHP Version: 4.3.0-dev OS: Linux
Private report: No CVE-ID: None
 [2002-02-12 08:59 UTC] 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
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-07-03 20:06 UTC] 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-07-03 20:29 UTC] 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 21:16 UTC] eru@php.net
Got me convinced: re-categorizing, -versioning, etc.

 [2011-08-23 14:27 UTC] datibbaw@php.net
-Status: Open +Status: Closed -Package: Feature/Change Request +Package: *General Issues -Assigned To: +Assigned To: datibbaw
 [2011-08-23 14:27 UTC] datibbaw@php.net
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
)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Jun 01 14:01:29 2024 UTC