php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41686 Omitting length param in array_slice not possible
Submitted: 2007-06-14 10:10 UTC Modified: 2007-06-18 16:53 UTC
Votes:2
Avg. Score:3.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: rele at gmx dot de Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 5.2.3 OS: Windows XP SP2
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: rele at gmx dot de
New email:
PHP Version: OS:

 

 [2007-06-14 10:10 UTC] rele at gmx dot de
Description:
------------
array_slice supports omitting of the $length parameter only if you pass just the first two parameters (params 1 and 2), but passing the default value NULL when you want to set $preserve_keys to TRUE (params 1, 2, 3 and 4) does not work.
This would be especially useful for associative arrays.

Reproduce code:
---------------
$a = array(1,2,3);
$b = array('a'=>1,'b'=>1,'c'=>2);
var_dump(array_slice($a, 1), array_slice($a, 1, 2, TRUE), array_slice($a, 1, NULL, TRUE), array_slice($b, 1), array_slice($b, 1, 2, TRUE), array_slice($b, 1, NULL, TRUE));

Expected result:
----------------
array(2) {
  [0]=>
  int(2)
  [1]=>
  int(3)
}
array(2) {
  [1]=>
  int(2)
  [2]=>
  int(3)
}
array(2) {
  [1]=>
  int(2)
  [2]=>
  int(3)
}
array(2) {
  ["b"]=>
  int(1)
  ["c"]=>
  int(2)
}
array(2) {
  ["b"]=>
  int(1)
  ["c"]=>
  int(2)
}
array(2) {
  ["b"]=>
  int(1)
  ["c"]=>
  int(2)
}

Actual result:
--------------
array(2) {
  [0]=>
  int(2)
  [1]=>
  int(3)
}
array(2) {
  [1]=>
  int(2)
  [2]=>
  int(3)
}
array(0) {
}
array(2) {
  ["b"]=>
  int(1)
  ["c"]=>
  int(2)
}
array(2) {
  ["b"]=>
  int(1)
  ["c"]=>
  int(2)
}
array(0) {
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-06-14 15:14 UTC] rele at gmx dot de
I could reproduce the same result with PHP 5.2.1 on SuSE Linux Enterprise Server 9.
 [2007-06-18 16:53 UTC] iliaa@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: Thu Nov 21 10:01:29 2024 UTC