array_sum 的更简单方法?

Easier way of doing the array_sum?

我使用的代码对我来说工作得很好。我目前遇到的问题更像是一种更简单的方法来做我想做的事情。我为每个需要汇总为最终总值的数组创建了变量。有没有办法让我不必在数组中有那么多变量?

这是我目前拥有的,在我看来它完成了工作,但它看起来像废话。方式多变。

$a1 = $aDataTableDetailHTML[0]['Players'];
$a2 = $aDataTableDetailHTML[1]['Players'];
$a3 = $aDataTableDetailHTML[2]['Players'];
$a4 = $aDataTableDetailHTML[3]['Players'];
$a5 = $aDataTableDetailHTML[4]['Players'];
$a6 = $aDataTableDetailHTML[5]['Players'];
$a7 = $aDataTableDetailHTML[6]['Players'];
$a8 = $aDataTableDetailHTML[7]['Players'];
$a9 = $aDataTableDetailHTML[8]['Players'];
$a10 = $aDataTableDetailHTML[9]['Players'];
$a11 = $aDataTableDetailHTML[10]['Players'];
$a12 = $aDataTableDetailHTML[11]['Players'];
$a13 = $aDataTableDetailHTML[12]['Players'];
$a14 = $aDataTableDetailHTML[13]['Players'];
$a15 = $aDataTableDetailHTML[14]['Players'];
$a16 = $aDataTableDetailHTML[15]['Players'];
$a17 = $aDataTableDetailHTML[16]['Players'];
$a18 = $aDataTableDetailHTML[17]['Players'];
$a19 = $aDataTableDetailHTML[18]['Players'];
$a20 = $aDataTableDetailHTML[19]['Players'];
$a21 = $aDataTableDetailHTML[20]['Players'];
$a22 = $aDataTableDetailHTML[21]['Players'];
$a23 = $aDataTableDetailHTML[22]['Players'];
$a24 = $aDataTableDetailHTML[23]['Players'];
$a25 = $aDataTableDetailHTML[24]['Players'];
$a26 = $aDataTableDetailHTML[25]['Players'];
$a27 = $aDataTableDetailHTML[26]['Players'];
$a28 = $aDataTableDetailHTML[27]['Players'];
$a29 = $aDataTableDetailHTML[28]['Players'];
$a30 = $aDataTableDetailHTML[29]['Players'];
$a31 = $aDataTableDetailHTML[30]['Players'];
$a32 = $aDataTableDetailHTML[31]['Players'];
$totalplayers = array($a1, $a2, $a3, $a4, $a5, $a6, $a7, $a8, $a9, $a10, $a11, $a12, $a13, $a14, $a15, $a16, $a17, $a18, $a19, $a20, $a21, $a22, $a23, $a24, $a25, $a26, $a27, $a28, $a29, $a30, $a31, $a32);
$value = array_sum($totalplayers);

使用 foreach 的普通老方法(4 行而不是 34):

$sum = 0;
foreach ($aDataTableDetailHTML as $item) {
    $sum += $item['Players'];
}

根据@PaulCrovella 在我原来的 post 中评论的内容,我能够想出一些非常有效的东西。

$values = array_column($aDataTableDetailHTML, 'Players');
$total = array_sum($values);
print_r($total);
echo array_sum(array_column($aDataTableDetailHTML, 'Players'));