Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在多维数组中将php数组的值相加_Php_Arrays - Fatal编程技术网

在多维数组中将php数组的值相加

在多维数组中将php数组的值相加,php,arrays,Php,Arrays,我在php中有一个数组,如下所示 Array ( [0] => Array ( [user] => 1 ) ) Array ( [0] => Array ( [vote] =>

我在php中有一个数组,如下所示

     Array
       (
           [0] => Array
               (
                   [user] => 1
               )           
       )
       Array
       (
           [0] => Array
               (
                   [vote] => 3.0
               )

           [1] => Array
               (
                   [vote] => 5.0
               )           
       )
我需要将数组中的vote值(3.0+5.0=8)相加


在php中执行此操作的最佳方法是什么?也许您可以使用以下方法:

$sum = $the_second_array[0];
for($i=1; $i<count($the_second_array); $i++){
  $sum += $the_second_array[$i];
}
$sum=$the_second_数组[0];

对于($i=1;$i也许您可以使用此选项:

$sum = $the_second_array[0];
for($i=1; $i<count($the_second_array); $i++){
  $sum += $the_second_array[$i];
}
$sum=$the_second_数组[0];
对于($i=1;$i试试这个:

//assuming $array is your array...
$score = 0;
foreach($array[1] as $element)
{
    foreach($element as $key=>$val)
    {
        if('vote'==$key)
        {
             $score+=$val;
        }
    }
}
试试这个:

//assuming $array is your array...
$score = 0;
foreach($array[1] as $element)
{
    foreach($element as $key=>$val)
    {
        if('vote'==$key)
        {
             $score+=$val;
        }
    }
}

要添加投票的值,可以执行以下操作:

array_sum(array_column($array[1], 'vote'));

要添加投票的值,可以执行以下操作:

array_sum(array_column($array[1], 'vote'));


是否只添加键为“投票”的数组的值?是否只添加键为“投票”的数组的值?