how to do a sum on a string in php?(如何在php中对字符串进行求和?)
问题描述
我有一个简单的问题:
我有这个变量:$v = "24000,1500,1500,1500,1500,1500,";
我想把这些数字加在一起.
i would like to add those numbers together.
我尝试用 +
等 eval()
替换 str_replace
,
,但是没用.
i've tried to str_replace
the ,
with +
and so a eval()
, but that didn't worked.
我也试过 str_split()
但它不知道在 ,
上分割.
i also tried str_split()
but it doesn't know to split on the ,
.
也许如果以某种方式将其转换为数组并执行 array_sum
...
maybe if somehow convert it to an array and do a array_sum
...
有什么想法吗?
谢谢
推荐答案
$sum = array_sum( explode( ',', $v ) );
这样做是将 $v
用分隔符 ,
和 分开explode()
并将结果数组与 array_sum()
.
What this does is split $v
by the delimiter ,
with explode()
and sum the resulting array of parts with array_sum()
.
这篇关于如何在php中对字符串进行求和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在php中对字符串进行求和?


- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01
- Laravel 仓库 2022-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01