本文介绍了如何在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 ) );

这是用分隔符, explode() $v拆分为$v,并将得到的零件数组求和与 array_sum() .

What this does is split $v by the delimiter , with explode() and sum the resulting array of parts with array_sum().

这篇关于如何在php中的字符串上求和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 18:59