php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42566 array_reduce coverts '' to 0 for initial value
Submitted: 2007-09-05 14:59 UTC Modified: 2007-10-24 07:37 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: lindsay at marshall dot name Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.2.4 OS: Fedora
Private report: No CVE-ID: None
 [2007-09-05 14:59 UTC] lindsay at marshall dot name
Description:
------------
If you want to use array_reduce to produce a string rather than a number you cannot set the initial string to be '' as it is converted to 0. You can work round this but it is not elegant. There seems to be no reason why array_reduce should not work with strings (it did in 4.x)

Reproduce code:
---------------
see comment on array_reduce page

Expected result:
----------------
see comment on array_reduce page


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-10-23 21:41 UTC] a at b dot c dot de
A short but complete example (copied from the cited user notes on the array_reduce page):

<?php
function arc ($reduced, $item)
{
  $reduced = $item.$reduced;
  return $reduced;
}

array_reduce( array(a,b,c), "arc", "" );
?>

Expected result: cba
Actual result: cba0
 [2007-10-24 07:37 UTC] scottmac@php.net
The prototype for the function clearly indicates an integer for the initial value, the enforcement was added prior to PHP 4.3.10 to fix bug #29954
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 12:01:30 2024 UTC