在多维数组中将php数组的值相加
我在php中有一个数组,如下所示在多维数组中将php数组的值相加,php,arrays,Php,Arrays,我在php中有一个数组,如下所示 Array ( [0] => Array ( [user] => 1 ) ) Array ( [0] => Array ( [vote] =>
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'));
是否只添加键为“投票”的数组的值?是否只添加键为“投票”的数组的值?