【问题标题】:PHP Get total of every n elements in arrayPHP获取数组中每n个元素的总数
【发布时间】:2021-10-04 03:22:36
【问题描述】:

我正在使用一个返回类似于下面数组的 JSON 的 API。如图所示,每个 ID 都是十进制的。我需要打印出 ID.x 的每个元素(根据 ID 的整数)和每个元素的标记,然后是总数。 (例如输出:
ID 0.1 = 2 mks
ID 0.2 = 4 mks
ID 0.3 = 1 mks
ID 0 总计 = 7

ID 1.1 = ........)。

<?php
$sampleArray = array(
  
    array("id"=>0.1, "marks"=>2),
    array("id"=>0.2, "marks"=>4),
    array("id"=>0.3, "marks"=>1),
    array("id"=>1.1, "marks"=>6),
    array("id"=>1.2, "marks"=>0),
    array("id"=>1.3, "marks"=>8),
    array("id"=>2.1, "marks"=>7),
    array("id"=>2.2, "marks"=>12),
);
 
$arrayLength = count($sampleArray);

$i = 0;
$x = 0;

while ($i < $arrayLength){
    $sum = 0;
 
    $idWhole = floor($sampleArray[$i]['id']);
    if(idWhole == $x){
        $sum += $sampleArray[$i]['marks'];
        echo 'id: '.$sampleArray['id'].', marks: '.$sampleArray[$i]['marks'].'<br>';
    }else{
        echo '<br>Sum of ID $idWhole is $sum<br>'; 
        $x++;
    }  
    $i++;
}

?>

【问题讨论】:

  • 只需将该 ID 值转换为整数,然后在其上实现经典的 control break ...?
  • idWhole without $ 是您的代码中的错字

标签: php arrays json multidimensional-array associative-array


【解决方案1】:

好消息,您不需要强制转换任何东西——当您将浮点值用作输出数组中的键时,PHP 会强制浮点值为整数(截断/取整值)。

循环时求和。

代码:(Demo)

$result = [];
foreach ($sampleArray as $row) {
    $result[$row['id']] = ($result[$row['id']] ?? 0) + $row['marks'];
}
var_export($result);

输出:

array (
  0 => 7,
  1 => 14,
  2 => 19,
)

从 PHP7.3 开始,您可以使用array_key_last() 来简化条件回显。 (Demo)

$result = [];
foreach ($sampleArray as $row) {
    if ($result && !isset($result[$row['id']])) {  // $result is not empty and current id is not yet in output array
        $lastKey = array_key_last($result);
        echo "Sum of ID $lastKey is {$result[$lastKey]}<br><br>";
    }
    echo "id: {$row['id']}, marks: {$row['marks']}<br>";
    $result[$row['id']] = ($result[$row['id']] ?? 0) + $row['marks'];
}
if ($result) {
    echo "Sum of ID {$row['id']} is {$result[$row['id']]}";
}

输出:

id: 0.1, marks: 2
id: 0.2, marks: 4
id: 0.3, marks: 1
Sum of ID 0 is 7

id: 1.1, marks: 6
id: 1.2, marks: 0
id: 1.8, marks: 8
Sum of ID 1 is 14

id: 2.1, marks: 7
id: 2.2, marks: 12
Sum of ID 2 is 19

【讨论】:

  • 非常感谢。这很好用。我真的很感激
【解决方案2】:
<?php
$txt = "PHP";
$sampleArray = array(
  array("id"=>0.1, "marks"=>2),
  array("id"=>0.2, "marks"=>4),
  array("id"=>0.3, "marks"=>1),
  array("id"=>1.1, "marks"=>6),
  array("id"=>1.2, "marks"=>0),
  array("id"=>1.3, "marks"=>8),
  array("id"=>2.1, "marks"=>7),
  array("id"=>2.2, "marks"=>12),
);

$total = 0;
$index = 0;
foreach($sampleArray as $item) {
    if($index == 0) { (int)$item['id']; }
    if((int)$item['id'] != $index){
        echo "<b>ID {$index} = {$total} mks</b></br>";
        $index = (int)$item['id'];
        $total = 0;
    }
    echo "ID {$item['id']} = {$item['marks']} mks</br>";
    $total += $item['marks']; 
}
echo "<b>ID {$index} = {$total} mks</b>";

?>

输出:

ID 0.1 = 2 mks
ID 0.2 = 4 mks
ID 0.3 = 1 mks
ID 0 = 7 mks
ID 1.1 = 6 mks
ID 1.2 = 0 mks
ID 1.3 = 8 mks
ID 1 = 14 mks
ID 2.1 = 7 mks
ID 2.2 = 12 mks
ID 2 = 19 mks

【讨论】:

  • 这个答案缺少教育解释。
猜你喜欢
  • 2011-07-08
  • 2021-10-05
  • 2020-07-21
  • 2019-09-20
  • 1970-01-01
  • 2011-04-12
  • 1970-01-01
  • 1970-01-01
  • 2020-11-03
相关资源
最近更新 更多