从数组计数值 [英] Counting values from an array
本文介绍了从数组计数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想计数一个数组内的所有值,我试过使用count()但没有效果,数组值也可能有负数,例如:
数组
(
[name] => 1
[phone] => 1
[emailX] => 1
[car] => 0
[finance] => 2
[employed] => 1
[credit] => -5
)
如果将它们全部统计在一起,结果应为1,你可以使用 .net / array_sumrel =nofollow noreferrer> array_sum():
$ sum = array_sum myArray);
count()函数给出了数组中的项数。
I'm trying to count all the values inside an array, I've tried using count() but to no avail, the array values may also have negatives in them, example:
Array
(
[name] => 1
[phone] => 1
[emailX] => 1
[car] => 0
[finance] => 2
[employed] => 1
[credit] => -5
)
If you count all of them together, the result should be 1, I'm just not sure how to go about it.. :/
解决方案
You can use array_sum():
$sum = array_sum($myArray);
The count() function gives you the number of items in the array.
这篇关于从数组计数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文