note 85548 deleted from function.array-sum by levim
| From: | levim@php.net | Date: | Tue, 18 Mar 2014 19:43:16 +0000 |
| Subject: | note 85548 deleted from function.array-sum by levim | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-198966@lists.php.net to get a copy of this message | ||
Note Submitter: c dot heckemueller at befestigungsfuchs dot de
----
<?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.