#36613 [Ver->Csd]: array_slice Cannot Preserve Keys without Length
| From: | felipe@php.net | Date: | Tue, 19 Feb 2008 00:16:40 +0000 |
| Subject: | #36613 [Ver->Csd]: array_slice Cannot Preserve Keys without Length | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-122438@lists.php.net to get a copy of this message | ||
ID: 36613
Updated by: felipe@php.net
Reported By: lists at zaunere dot com
-Status: Verified
+Status: Closed
Bug Type: Feature/Change Request
Operating System: Windows
PHP Version: 5.1.2
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2006-03-05 03:22:07] 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 {
------------------------------------------------------------------------
[2006-03-04 21:27:47] 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) {
}
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=36613&edit=1