PHP Snippets
array_sum_assoc
Information
(PHP Version >= 3)Functions used in this snippet: is_array, sizeof, array_sum_assoc.
Description
int array_sum_assoc( array array )
Sum's up all the values in the array array associatively.
Snippet
<?php
function array_sum_assoc($array){
if(!is_array($array) || sizeof($array) === 0){
return false;
}
$count = 0;
foreach($array as $key=>$value){
if(is_array($value)){
$count += array_sum_assoc($value);
}else{
$count += $value;
}
}
return $count;
}
?>
Example
array_sum_assoc Can be used in the following way:
<?php
// make an associative array
$array = array(1,array(1),array(1,array(1)));
echo array_sum_assoc($array);
// outputs 4, array_sum would output 1
?>
0 Responses to array_sum_assoc