Heim  >  Artikel  >  Backend-Entwicklung  >  PHP计算数组中值的和与乘积的方法(array_sum与array_product函数)_php技巧

PHP计算数组中值的和与乘积的方法(array_sum与array_product函数)_php技巧

WBOY
WBOYOriginal
2016-05-16 19:55:122160Durchsuche

本文实例讲述了PHP计算数组中值的和与乘积的方法。分享给大家供大家参考,具体如下:

一、概述:

array_sum() 函数用于计算数组中所有值的和。
array_product() 函数用于计算数组中所有值的乘积。

二、使用示例:

array_sum()

PHP array_sum() 函数用于计算数组中所有值的和,以整数或浮点数返回计算结果,非数字的单元将视作 0 。

语法:

number array_sum( array array )

例子:

<&#63;php
$arr_a = array(1, 2, 3, "a");
echo array_sum($arr_a);  //输出:6
&#63;>

array_product()

PHP array_product() 函数用于计算数组中所有值的乘积,以整数或浮点数返回计算结果,非数字的单元将视作 0 。

语法:

number array_product( array array )

例子:

<&#63;php
$arr_a = array(1, 2, 3);
echo array_product($arr_a);  //输出:6
$arr_b = array(1, 2, 3, "abc");
echo array_product($arr_b);  //输出:0
&#63;>

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn