©
このドキュメントでは、 php中国語ネットマニュアル リリース
(PHP 4 >= 4.0.4, PHP 5, PHP 7)
array_sum — 计算数组中所有值的和
$array
)array_sum() 将数组中的所有值的和以整数或浮点数的结果返回。
array
输入的数组。
所有值的和以整数或浮点数的结果返回。
版本 | 说明 |
---|---|
4.2.1 | PHP 4.2.1 之前的版本修改了传入的数组本身,将其中的字符串值转换成数值(大多数情况下都转换成了零,根据具体值而定)。 |
Example #1 array_sum() 例子
<?php
$a = array( 2 , 4 , 6 , 8 );
echo "sum(a) = " . array_sum ( $a ) . "\n" ;
$b = array( "a" => 1.2 , "b" => 2.3 , "c" => 3.4 );
echo "sum(b) = " . array_sum ( $b ) . "\n" ;
?>
以上例程会输出:
sum(a) = 20 sum(b) = 6.9
[#1] didatus at dynarize dot de [2014-09-20 21:21:17]
If you want to check if there are for example only strings in an array, you can use a combination of array_sum and array_map like this:
<?php
function only_strings_in_array($arr) {
return array_sum(array_map('is_string', $arr)) == count($arr);
}
$arr1 = array('one', 'two', 'three');
$arr2 = array('foo', 'bar', array());
$arr3 = array('foo', array(), 'bar');
$arr4 = array(array(), 'foo', 'bar');
var_dump(
only_strings_in_array($arr1),
only_strings_in_array($arr2),
only_strings_in_array($arr3),
only_strings_in_array($arr4)
);
?>
This will give you the following result:
bool(true)
bool(false)
bool(false)
bool(false)
[#2] hdeus at yahoo dot com [2008-10-06 07:01:29]
Here is how you can multiply two arrays in the form of matrixes using a bit of matrix algebra (M*M).
By calling the function multiplyMatrix, you will be multiplying two sparse matrixes (zeros need not be included in the array for the operation to be performed).
<?php
$M = array(
0=>array(1=>1,4=>1),
1=>array(2=>1,3=>1),
3=>array(1=>1),
4=>array(5=>1),
5=>array(6=>1)
);
$M1 = multiplyMatrix($M, $M); //multiplying $M by itself
echo '<pre>';print_r($M1);echo '</pre>';
function multiplyMatrix($M1, $M2)
{
#Helena F Deus, Oct 06, 2008
##Multiply two matrixes; $M1 and $M2 can be sparse matrixes, the indexes on both should match
if(is_file($M1)) {$matrix1 = unserialize(file_get_contents($M1));}
else $matrix1 = $M1;
#transpose M2
$M2t = transpose($M2);
foreach ($M2t as $row=>$tmp) {
##sum the result of the value in the col multiplied by the value in the vector on the corresponding row
foreach ($M1 as $row1=>$tmp1) {
$multiply[$row1] = array_rproduct($tmp,$tmp1);
if(!$multiply[$row1]){
exit;
}
}
foreach ($multiply as $row1=>$vals) {
$sum[$row][$row1]=array_sum($vals);
}
}
$r=transpose($sum);
return ($r);
}
function transpose($M)
{
foreach ($M as $row=>$cols) {
foreach ($cols as $col=>$value) {
if($value)
$Mt[$col][$row]=$value;
}
}
ksort($Mt);
return ($Mt);
}
function array_rproduct($a1, $a2)
{
foreach ($a1 as $line=>$cols) {
$a3[$line] = $a1[$line]*$a2[$line];
foreach ($a2 as $line2=>$cols2) {
$a3[$line2] = $a1[$line2]*$a2[$line2];
}
}
ksort($a3);
return ($a3);
}
?>
[#3] herenvardo at gmail dot com [2006-11-24 15:28:24]
I'm not sure if something similar already exists, but I needed it so I made it:
<?php
function array_pitag_sum($arr) {
if(is_array($arr) {
$ret = 0;
foreach($arr as $i) {
if(is_array($i)) {
$s = array_sum($i);
$ret += $s*$s;
} else {
$ret += $i*$i;
}
}
return floor(sqrt($ret));
} else {
return $arr;
}
}
?>
[#4] punchto at hotmail dot com [2005-03-15 19:06:53]
Microsoft Excel - SUMIF()
function sumif($array,$criteria,$sum_array){
if(is_array($array) && is_array($sum_array) && trim($criteria)!= ""){
$array_count = (count($array) < count($sum_array)) ? count($array):count($sum_array);
for($i=0;$i<$array_count;$i++){
if(ereg("^<",$criteria)){
$value = ereg_replace("^<","",$criteria);
$result += $array[$i] < $value ? $sum_array[$i]:0;
}
elseif(ereg("^>",$criteria)){
$value = ereg_replace("^>","",$criteria);
$result += $array[$i] > $value ? $sum_array[$i]:0;
}
else{
$value = $criteria;
$result += $array[$i] == $value ? $sum_array[$i]:0;
}
}
return $result ? $result:0;
}
}
[#5] ncheung at maine dot rr dot com [2005-02-07 18:02:53]
For clarity, array indices containing boolean values such as TRUE and FALSE are added up as though they are 1 and 0 respectively.
[#6] mucello NOO SPAM @ weatherimages dOt org [2003-10-22 15:44:51]
If you want to find the AVERAGE of the values in your array, use the sum and count functions together. For example, let's say your array is $foo and you want the average...
<?php
$average_of_foo = array_sum($foo) / count($foo);
?>
[#7] drverieevil at REMOVEMEasenne dot org [2003-07-30 03:14:29]
If some array elements arent integers, function will change them to integers (content of array will not change) type and then sum them.
Example:
<?php
$foo[] = "12";
$foo[] = 10;
$foo[] = "bar";
$foo[] = "summer";
echo array_sum ($foo); //same as echo "22";
?>