在php中使用数组 [英] Working with arrays in php

查看:55
本文介绍了在php中使用数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法来获取PHP数组中所有项目的总数?

Is there a simple way to get the total of all items in a PHP array?

此外,我如何输出数组的内容以进行调试?

Also, how can I output the contents of an array for debugging purposes?

推荐答案

您正在寻找的是array_sum

What you are looking for is array_sum http://uk.php.net/manual/en/function.array-sum.php

array_sum —计算数组中值的总和

array_sum — Calculate the sum of values in an array

要输出数组的内容,请使用 var_dump print_r .例如

To output the contents of an array use var_dump or print_r. e.g

$myarr = array(1,5,2,7,6);

echo "<pre>";
print_r($myarr);
echo "</pre>";

echo "The Sum of my array is ".array_sum($myarr);

// Output

Array
(
    [0] => 1
    [1] => 5
    [2] => 2
    [3] => 7
    [4] => 6
)

The Sum of my array is 21

这篇关于在php中使用数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆