php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #36613 array_slice Cannot Preserve Keys without Length
Submitted: 2006-03-04 21:27 UTC Modified: 2008-02-19 00:16 UTC
From: lists at zaunere dot com Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 5.1.2 OS: Windows
Private report: No CVE-ID: None
 [2006-03-04 21:27 UTC] lists at zaunere dot com
Description:
------------
The optional fourth parameter preserve_keys is only available if the third parameter length is also supplied.  If the developer wishes to set preserve_keys to TRUE, he must also supply the length of the array he wishes to have returned, which may not be known.

There is no way to indicate that length should remain optional - like passing NULL - while providing a value for preserve_keys.

Reproduce code:
---------------
$SomeArray = array('First','Middle','Last');

var_dump(array_slice($SomeArray,1,NULL,TRUE));

Expected result:
----------------
array(2) {
  [1]=>
  string(6) "Middle"
  [2]=>
  string(4) "Last"
}


Actual result:
--------------
array(0) {
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-03-05 03:22 UTC] bjori@php.net
Workaround: array_slice($someArray, 1, count($someArray), true);
Patch: http://php.is/bugs/36613/array_slice.patch.txt

Index: array.c
===================================================================
RCS file: /repository/php-src/ext/standard/array.c,v
retrieving revision 1.308.2.18
diff -u -r1.308.2.18 array.c
--- array.c	26 Feb 2006 10:49:50 -0000	1.308.2.18
+++ array.c	5 Mar 2006 02:20:28 -0000
@@ -2183,7 +2183,7 @@
 	   is not passed */
 	convert_to_long_ex(offset);
 	offset_val = Z_LVAL_PP(offset);
-	if (argc >= 3) {
+	if (argc == 3 || (argc == 4 && Z_TYPE_PP(length) != IS_NULL)) {
 		convert_to_long_ex(length);
 		length_val = Z_LVAL_PP(length);
 	} else {

 [2008-02-19 00:16 UTC] felipe@php.net
This bug has been fixed in CVS.

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/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 07 20:01:32 2024 UTC