gpt4 book ai didi

PHP 数组逻辑重构

转载 作者:搜寻专家 更新时间:2023-10-31 21:18:40 25 4
gpt4 key购买 nike

PHP 数组逻辑重构

我正在尝试重构我的代码...这是 PHP ...

我有以下内容:

$totals[] = "Total";
$totals[] = $counts['hole'][1] + $counts['warn'][1] + $counts['info'][1] + $counts['crit'][1];
$totals[] = $counts['hole'][2] + $counts['warn'][2] + $counts['info'][2] + $counts['crit'][2];
$totals[] = $counts['hole'][3] + $counts['warn'][3] + $counts['info'][3] + $counts['crit'][3];
$totals[] = $counts['hole'][4] + $counts['warn'][4] + $counts['info'][4] + $counts['crit'][4];
$totals[] = $counts['hole'][5] + $counts['warn'][5] + $counts['info'][5] + $counts['crit'][5];
$totals[] = $counts['hole'][6] + $counts['warn'][6] + $counts['info'][6] + $counts['crit'][6];

为什么这行不通?

for($i; $i < 6; $i++ ){
foreach( $severity as $sev ){
$totals[$i] = $totals[$i] + $counts[$sev][$i];
}
}

最佳答案

你在 for 循环中有一个错误:

for ($i = 1; $i <= 6; $i++) {
foreach ($severity as $sev) {
$totals[$i] += $counts[$sev][$i];
}
}

您忘记初始化 $i 变量。

关于PHP 数组逻辑重构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2747090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com