0

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

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

$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);
4

3 回答 3

3

根据@PaulCrovella 在我原来的帖子中的评论,我能够想出一些效果很好的东西。

$values = array_column($aDataTableDetailHTML, 'Players');
$total = array_sum($values);
print_r($total);
于 2018-12-24T08:42:06.360 回答
2

一种简单的旧方式foreach4行而不是34行):

$sum = 0;
foreach ($aDataTableDetailHTML as $item) {
    $sum += $item['Players'];
}
于 2018-12-24T08:29:37.917 回答
1
echo array_sum(array_column($aDataTableDetailHTML, 'Players'));
于 2018-12-24T09:16:14.937 回答