note 90015 added to function.array-reduce
| From: | Hrobky at osu1 dot php dot net | Date: | Wed, 01 Apr 2009 22:34:48 +0000 |
| Subject: | note 90015 added to function.array-reduce | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-152730@lists.php.net to get a copy of this message | ||
The initial value have to be int. So it is impossible to implement non-recursive flatten like this:
<?php
$arr = array(array(1), array(2), array(3));
$result = array_reduce($arr, 'array_merge', array());
// expected: $result == array (1, 2, 3)
?>
I get: Warning: array_merge() : Argument #1 is not an array.
If $initial parameter is not an int, array_reduce passes 0 (zero) to the callback function. Hayley
Watson's note suggests a solution:
<?php
$acc = array();
foreach($arr as $a)
$acc = array_merge($acc, $a);
// now: $acc == array (1, 2, 3)
?>
----
Server IP: 217.73.17.12
Probable Submitter: 213.215.89.249
----
Manual Page -- http://www.php.net/manual/en/function.array-reduce.php
Edit -- https://master.php.net/note/edit/90015
Del: integrated -- https://master.php.net/note/delete/90015/integrated
Del: useless -- https://master.php.net/note/delete/90015/useless
Del: bad code -- https://master.php.net/note/delete/90015/bad+code
Del: spam -- https://master.php.net/note/delete/90015/spam
Del: non-english -- https://master.php.net/note/delete/90015/non-english
Del: in docs -- https://master.php.net/note/delete/90015/in+docs
Del: other reasons-- https://master.php.net/note/delete/90015
Reject -- https://master.php.net/note/reject/90015
Search -- https://master.php.net/manage/user-notes.php