Voting

: max(eight, six)?
(Example: nine)

The Note You're Voting On

ruslan dot zavackiy at gmail dot com
12 years ago
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:

<?php
$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);
?>

<< Back to user notes page

To Top