Home >Backend Development >PHP Tutorial >应该怎么取出数组中某个值到某个值之间的总和呢?

应该怎么取出数组中某个值到某个值之间的总和呢?

WBOY
WBOYOriginal
2016-06-06 20:44:071122browse

比如这样

<code class="lang-php"><br>$a = array(11,32,54,321,26,47,312,4,45,546,87,534,54,56,);

</code>

这个数组,我想算出$a[2]到$a[10]中间所有值的总和,除了一个个赋值加过来,有更方便的方法吗?

回复内容:

比如这样

<code class="lang-php"><br>$a = array(11,32,54,321,26,47,312,4,45,546,87,534,54,56,);

</code>

这个数组,我想算出$a[2]到$a[10]中间所有值的总和,除了一个个赋值加过来,有更方便的方法吗?

array_slice();取出一段你需要的数组
然后 array_sum();计算总和啊。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn