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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: lindsay at marshall dot name
New email:
PHP Version: OS:

 

 [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: Mon May 06 19:01:32 2024 UTC