★ wanayoo — archive 1999 http://fr.php.net/manual/ja/function.array-sum.phpNouvelle recherche | Portail wanayoo
PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<array_splicearray_udiff_assoc>
view the version of this page
Last updated: Thu, 10 Mar 2005

array_sum

(PHP 4 >= 4.0.4, PHP 5)

array_sum --  配列の中の値の合計を計算する

説明

mixed array_sum ( array arr)

array_sum() は、配列の中の値の合計を整数または floatとして返します。

例 1. array_sum() の例

<?php
$a
= array(2,4,6,8);
echo
"sum(a) = ".array_sum($a)."\n";
// 出力: sum(a) = 20

$b = array("a"=>1.2,"b"=>2.3,"c"=>3.4);
echo
"sum(b) = ".array_sum($b)."\n";
// 出力: sum(b) = 6.9
?>

上のプログラムは次のように出力されます:

sum(a) = 20
sum(b) = 6.9

注意: 4.0.6より前のバージョンのPHPは、渡された配列自体を修正し、文字列を 数値(これは値にもよるが多くの場合ゼロとなります)に変換していました。



add a note add a note User Contributed Notes
array_sum
punchto at hotmail dot com
16-Mar-2005 04:06
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;
  }
}
adam at laural dot com dot au
02-Mar-2005 01:37
Here's a function to return the sum of a portion of an array:

function array_partial_sum($array, $start, $length){
   $new_array = array_slice($array, $start, $length);
   return array_sum($new_array);
}

$array = array(1, 2, 3, 4, 5);
print array_partial_sum($array, 0, 3);  // prints 6
ncheung at maine dot rr dot com
08-Feb-2005 03:02
For clarity, array indices containing boolean values such as TRUE and FALSE are added up as though they are 1 and 0 respectively.
info at sgonda dot de
08-Dec-2003 12:54
Don't use number_format() before array_sum()...it won't work correctly, if you have a summary...
mucello NOO SPAM @ weatherimages dOt org
23-Oct-2003 12:44
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);
?>
mcrm at NOTNEEDIT dot freemal dot it
13-Aug-2003 06:40
Hi people, if someone is searching a function that works also with multimension array, i suggest this :

<?php
function cw_array_count($a) {
  if(!
is_array($a)) return $a;
  foreach(
$a as $key=>$value)
    
$totale += cw_array_count($value);
  return
$totale;
}

$a[0][E][PS][P][a1]=1;
$a[0][E][PS][P][a2]=2;
$a[0][E][PJ][P][a2]=2;
$a[1][E][PS][E][a3]=3;

echo
cw_array_count($a[0]);
// or
echo cw_array_count($a[0][E]);
?>

Bye, Bye.
R.Martina
drverieevil at REMOVEMEasenne dot org
30-Jul-2003 12:14
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";
?>

<array_splicearray_udiff_assoc>
 Last updated: Thu, 10 Mar 2005
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2005 The PHP Group
All rights reserved.
This mirror generously provided by: nexen.net
Last updated: Fri Mar 18 05:13:58 2005 CET