如何在PHP中将所有数组值加在一起? [英] How can I add all of my array values together in PHP?

查看:281
本文介绍了如何在PHP中将所有数组值加在一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在PHP中将所有数组值加在一起?有功能吗?

How can I add all of my array values together in PHP? Is there a function for this?

推荐答案

如果数组由数字组成,则可以使用 array_sum 来计算总数.手册中的示例:

If your array consists of numbers, you can use array_sum to calculate a total. Example from the manual:

$a = array(2, 4, 6, 8);
echo "sum(a) = " . array_sum($a) . "\n";

如果数组由字符串组成,则可以使用 内插 :

If your array consists of strings, you can use implode:

implode(",", $array);

它将变成这样的数组:

strawberries
peaches
pears
apples

插入这样的字符串:

strawberries,peaches,pears,apples

这篇关于如何在PHP中将所有数组值加在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆