Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.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 在foreach循环中添加列的值。_Php_Mysql - Fatal编程技术网

Php 在foreach循环中添加列的值。

Php 在foreach循环中添加列的值。,php,mysql,Php,Mysql,我有两张不同的表格。我想添加这些数量,并将其更新到表3的“金额”列中。 我已经编写了这段代码,但是值显示为0 foreach($key=>$value的产品) { 我只是想知道如何在foreach循环中获得数组值的总和 得到两个数组的总和,其中包含如下数值: $sum = 0; foreach($amountArray as $amount) { $sum += $amount; } foreach($amountArray2 as $amount) { $sum += a

我有两张不同的表格。我想添加这些数量,并将其更新到表3的“金额”列中。 我已经编写了这段代码,但是值显示为0 foreach($key=>$value的产品) {


我只是想知道如何在foreach循环中获得数组值的总和

得到两个数组的总和,其中包含如下数值:

$sum = 0;
foreach($amountArray as $amount) {
     $sum += $amount;
}

foreach($amountArray2 as $amount) {
     $sum += amount;
}

// update with $sum...
1.)如果要查找某个特定数组的元素之和,可以使用array_sum()

2.)如果要获得两个数组的和,可以按照以下方法进行,前提是数组是数字索引的

for($index=0; $index < count($array1); $index++) {
   $array3[$index] = $array1[$index] + $array2[$index];
}

两个数组的元素数应该相等,这样它才能工作。

您在问题中输入的代码到底是什么?我不知道它与问题之间有什么联系。我只是想知道如何在foreach循环中获得数组值的和,那么您打算获得一个特定数组中所有元素的和吗。。。??
array_sum($array1);
for($index=0; $index < count($array1); $index++) {
   $array3[$index] = $array1[$index] + $array2[$index];
}
for ($counter=0;$counter<count($array1);$counter++) {
    $array3[$counter] = current($array1) + current($array2);
    next($array1); next($array2);
}