php - PHP 7“不能使用字符串偏移作为数组”
问题描述
我们最近升级到 Php 7.4 并遇到以下代码块的潜在致命错误警报:
$total = 0;
$balance = 0;
if (is_array($company['history']) && ($histCount = count($company['history']))) {
for ($i = $histCount - 1; $i >= 0; $i--) {
$total += $company['history'][$i]['amt_total'];
$balance += $company['history'][$i]['due'];
$company['history'][ $i ]['balance'] = $balance; // EA flags as potential fatal error
$company['history'][ $i ]['total'] = $total; // EA flags as potential fatal error
}
}
中的每个history
元素$company
都有(除其他外)amt_total
、、、和初始化due
,最后两个被初始化为。balance
total
0
EA 标记了指示的两行,说“可能引发 PHP 致命错误(不能将字符串偏移量用作数组)。”
感谢任何可以帮助我理解这里出了什么问题的人。
附加信息有人问定义的代码$company
来自哪里......
foreach ($this->orderList as $order) {
if ($order['amt_paid'] !== $order['real_amt_paid'] && !empty($order['real_amt_paid'])) {
$order['amt_paid'] = $order['real_amt_paid'];
}
$row = [];
foreach ($this->customerHistoryColumns as $col) {
$order[$col[0]] = ($order[$col[0]] === null ? "" : str_replace("&", "&", $order[$col[0]]));
switch ($col[0]) {
...
case "due":
$row['due'] = $order['amt_total'] - $order['amt_paid'];
break;
...
default:
$row[$col[0]] = $order[$col[0]];
break;
}
}
$row['balance'] = 0;
$row['total'] = 0;
$arr['history'][] = $row;
}
return $arr['history'];
公司本身在别处定义;这只是检索history
元素。
FWIW,代码多年来一直完美运行;是升级到 PHP7 引起了这个问题。
解决方案
推荐阅读
- php - 无法循环通过 PHP 中的 XML 节点
- java - API 级别 28 的 Android-ProviderTestCase2 不起作用
- python - 我如何让这个 RPG 工作,它告诉我 Y 和 N 没有定义
- excel - 如何让用户使用 Excel VBA 在多重回归中选择 X1、X2... 和 Y 的值范围
- c# - 从本机应用程序调用托管代码时,CLR 何时以及如何初始化?
- c# - Unity 3D c# 2 个不同的循环同时运行
- r - 为什么“矢量化”这个简单的 R 循环会产生不同的结果?
- ios - 渐进式 Web 应用的 iOS 默认启动 URL
- json - 我如何知道使用 jsonpath 表达式的元素总数?
- git - 用于版本控制的 Oracle SQL Developer 导出源