Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:36861 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 7572 invoked from network); 8 Apr 2008 01:17:43 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 8 Apr 2008 01:17:43 -0000 Authentication-Results: pb1.pair.com smtp.mail=cschneid@cschneid.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=cschneid@cschneid.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain cschneid.com from 195.226.6.51 cause and error) X-PHP-List-Original-Sender: cschneid@cschneid.com X-Host-Fingerprint: 195.226.6.51 darkcity.gna.ch Linux 2.6 Received: from [195.226.6.51] ([195.226.6.51:38048] helo=mail.gna.ch) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 85/72-24652-4B7CAF74 for ; Mon, 07 Apr 2008 21:17:41 -0400 Received: from localhost (localhost [127.0.0.1]) by darkcity.gna.ch (Postfix) with ESMTP id 7990C16ECD8 for ; Tue, 8 Apr 2008 03:17:37 +0200 (CEST) Received: from unknown by localhost (amavisd-new, unix socket) id client-XXJpMAHI for ; Tue, 8 Apr 2008 03:17:35 +0200 (CEST) Received: from [192.168.1.47] (84-72-92-81.dclient.hispeed.ch [84.72.92.81]) (using TLSv1 with cipher AES128-SHA (128/128 bits)) (No client certificate requested) by darkcity.gna.ch (Postfix) with ESMTP id C118716ECD2 for ; Tue, 8 Apr 2008 03:17:33 +0200 (CEST) Message-ID: <8A963872-3893-4944-A5BF-C227421C095B@cschneid.com> To: PHP Developers Mailing List Content-Type: multipart/mixed; boundary=Apple-Mail-8--470497282 Mime-Version: 1.0 (Apple Message framework v919.2) Date: Tue, 8 Apr 2008 03:17:32 +0200 X-Mailer: Apple Mail (2.919.2) X-Virus-Scanned: amavisd-new at gna.ch Subject: [PATCH] Allow mixed $initial in array_reduce From: cschneid@cschneid.com (Christian Schneider) --Apple-Mail-8--470497282 Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit I attached a patch and a phpt-file for a small patch to array_reduce to allow any type of $initial value. This patch is for HEAD but could easily be applied/ported to 5.3. - Chris --Apple-Mail-8--470497282 Content-Disposition: attachment; filename=array_reduce.patch.txt Content-Type: text/plain; x-unix-mode=0664; name="array_reduce.patch.txt" Content-Transfer-Encoding: 7bit Index: ext/standard/array.c =================================================================== RCS file: /repository/php-src/ext/standard/array.c,v retrieving revision 1.448 diff -u -r1.448 array.c --- ext/standard/array.c 12 Mar 2008 19:21:30 -0000 1.448 +++ ext/standard/array.c 8 Apr 2008 01:11:41 -0000 @@ -4219,7 +4219,7 @@ } /* }}} */ -/* {{{ proto mixed array_reduce(array input, mixed callback [, int initial]) U +/* {{{ proto mixed array_reduce(array input, mixed callback [, mixed initial]) U Iteratively reduce the array to a single value via the callback. */ PHP_FUNCTION(array_reduce) { @@ -4230,18 +4230,19 @@ zval *retval; zend_fcall_info fci; zend_fcall_info_cache fci_cache = empty_fcall_info_cache; - long initial; + zval *initial; HashPosition pos; HashTable *htbl; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "af|l", &input, &fci, &fci_cache, &initial) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "af|z", &input, &fci, &fci_cache, &initial) == FAILURE) { return; } if (ZEND_NUM_ARGS() > 2) { ALLOC_ZVAL(result); + *result = *initial; + zval_copy_ctor(result); INIT_PZVAL(result); - ZVAL_LONG(result, initial); } else { MAKE_STD_ZVAL(result); ZVAL_NULL(result); --Apple-Mail-8--470497282 Content-Disposition: attachment; filename=array_reduce.phpt.txt Content-Type: text/plain; x-unix-mode=0664; name="array_reduce.phpt.txt" Content-Transfer-Encoding: 7bit --TEST-- Test array_reduce() function --INI-- precision=14 --FILE-- 42, 'bar' => 17, 'qux' => -2, 'quux' => 0); var_dump(array_reduce($array, 'reduce_array', $initial), $initial); echo "\nDone"; ?> --EXPECTF-- *** Testing array_reduce() to integer *** int(61) int(42) *** Testing array_reduce() to float *** float(6.1) float(4.2) *** Testing array_reduce() to string *** string(23) "quuxfoofoobarquxquxquux" string(4) "quux" *** Testing array_reduce() to array *** array(4) { ["foo"]=> int(44) ["bar"]=> int(18) ["qux"]=> int(0) ["quux"]=> int(1) } array(4) { ["foo"]=> int(42) ["bar"]=> int(17) ["qux"]=> int(-2) ["quux"]=> int(0) } Done --Apple-Mail-8--470497282--