note 85548 added to function.array-sum

From: Date: Fri, 05 Sep 2008 08:30:39 +0000
Subject: note 85548 added to function.array-sum
Groups: php.notes 
Request: Send a blank email to php-notes+get-144153@lists.php.net to get a copy of this message
<?php /** * sum values in array * * @param array $arr * @param string [optional]$index * @return int result */ function array_sum_key( $arr, $index = null ){ if(!is_array( $arr ) || sizeof( $arr ) < 1){ return 0; } $ret = 0; foreach( $arr as $id => $data ){ if( isset( $index ) ){ $ret += (isset( $data[$index] )) ? $data[$index] : 0; }else{ $ret += $data; } } return $ret; } ?> With this function you can choose whether you sum specific keys in a multidim array, or a normalized array. it is designed to not trigger errors if a key is not set. ---- Server IP: 217.13.201.10 Probable Submitter: 84.130.209.70 ---- Manual Page -- http://www.php.net/manual/en/function.array-sum.php Edit -- https://master.php.net/note/edit/85548 Del: integrated -- https://master.php.net/note/delete/85548/integrated Del: useless -- https://master.php.net/note/delete/85548/useless Del: bad code -- https://master.php.net/note/delete/85548/bad+code Del: spam -- https://master.php.net/note/delete/85548/spam Del: non-english -- https://master.php.net/note/delete/85548/non-english Del: in docs -- https://master.php.net/note/delete/85548/in+docs Del: other reasons-- https://master.php.net/note/delete/85548 Reject -- https://master.php.net/note/reject/85548 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#144153) next »