如何在php中添加二维数组的值

时间:2014-09-13 03:58:42

标签: php arrays

我有以下给定的数组

Array
(
    [0] => 2,3   
    [1] => 2,3   
)

我想总结并获得变量的结果。例如。变量a = 4 (2+2=4)和变量b = 6 (3+3=6)。我在php编码。

2 个答案:

答案 0 :(得分:0)

试试这个

$a = 0;
$b =0;
foreach ($yourArr as $temp)
{
    $a += $temp[0];
    $b += $temp[1];
}
echo "a = ".$a."<br>";
echo "b = ".$b;

<强>输出

a = 4
b = 6

答案 1 :(得分:0)

在foreach中使用EXPLODE

<?php
$yourArr = array('2,3','2,3');
$a = 0;
$b =0;
foreach ($yourArr as $temp)
{
    $tempnew = explode(",",$temp);

    $a += $tempnew[0];
    $b += $tempnew[1];
}
echo "a = ".$a."<br>";
echo "b = ".$b;
?>