如何对多维数组中的元素进行计数和求和?
我有一个返回一些数字的数组。我想将这些数字加在一起并计算它们。
这是我到目前为止所做的:
<?php
$homepage = file_get_contents('http://graph.facebook.com/215844978437619/reviews');
$parsed = json_decode($homepage,true);
foreach ($parsed['data'] as $key => $values){
$totalRatings1 = $values['rating'] ;
}
?>
我想做的是将 $values[' rating']
相加并计算它们。
所以: $totalRatings = sum_array($values[' rating'])
和 $totalCount = count($values[' rating'])
但我迷失在语法。
有什么想法吗? 谢谢
i have a array that returns some numbers. and i want to add those numbers together and also count them.
here is what i have so far:
<?php
$homepage = file_get_contents('http://graph.facebook.com/215844978437619/reviews');
$parsed = json_decode($homepage,true);
foreach ($parsed['data'] as $key => $values){
$totalRatings1 = $values['rating'] ;
}
?>
what i am trying to do is to sum the $values['rating']
together and also count them.
So that: $totalRatings = sum_array($values['rating'])
and $totalCount = count($values['rating'])
but i get lost in the sintax.
any ideas?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
$totalRatings
将具有所有评级的汇总总和,$totalRated
将是有多少评级的计数。$totalRatings
will have the aggregated sum of all ratings,$totalRated
will be the count of how many ratings there are.只需声明两个变量,然后在每次迭代时递增它们:
$totalRating
将保存所有评级的总和,而$totalItems
将包含项目总数。Just declare two variables and then increment them on each iteration:
$totalRating
will hold the sum of all rating while$totalItems
will contain the total number of items.