note 109320 added to function.array-reduce
| From: | ruslan dot zavackiy at gmail dot com | Date: | Fri, 06 Jul 2012 08:29:46 +0000 |
| Subject: | note 109320 added to function.array-reduce | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-189046@lists.php.net to get a copy of this message | ||
If you want something elegant in your code, when dealing with reducing array, just unshift first
element, and use it as initial, because if you do not do so, you will + first element with first
element:
$arr = array(
array('min' => 1.5456, 'max' => 2.28548, 'volume' =>
23.152),
array('min' => 1.5457, 'max' => 2.28549, 'volume' =>
23.152),
array('min' => 1.5458, 'max' => 2.28550, 'volume' =>
23.152),
array('min' => 1.5459, 'max' => 2.28551, 'volume' =>
23.152),
array('min' => 1.5460, 'max' => 2.28552, 'volume' =>
23.152),
);
$initial = array_shift($arr);
$t = array_reduce($arr, function($result, $item) {
$result['min'] = min($result['min'], $item['min']);
$result['max'] = max($result['max'], $item['max']);
$result['volume'] += $item['volume'];
return $result;
}, $initial);
----
Server IP: 62.2.182.223
Probable Submitter: 194.8.15.14
----
Manual Page -- http://www.php.net/manual/en/function.array-reduce.php
Edit -- https://master.php.net/note/edit/109320
Del: integrated -- https://master.php.net/note/delete/109320/integrated
Del: useless -- https://master.php.net/note/delete/109320/useless
Del: bad code -- https://master.php.net/note/delete/109320/bad+code
Del: spam -- https://master.php.net/note/delete/109320/spam
Del: non-english -- https://master.php.net/note/delete/109320/non-english
Del: in docs -- https://master.php.net/note/delete/109320/in+docs
Del: other reasons-- https://master.php.net/note/delete/109320
Reject -- https://master.php.net/note/reject/109320
Search -- https://master.php.net/manage/user-notes.php