
本文详解如何安全、准确地遍历 php 中由 `json_decode($json, true)` 生成的深层嵌套数组,并以 html 表格形式输出物流扫描记录,避免“undefined array key”和“foreach() argument must be of type array|object”等常见错误。
在处理物流 API 返回的 JSON 数据(如 Delhivery 轨迹接口)时,json_decode($json, true) 会将其转换为多维关联数组。但直接按路径硬编码访问(如 $item['Shipment']['Scans']['ScanDetail'])极易出错——因为 Scans 本身是一个索引数组,其每个元素才是包含 'ScanDetail' 键的子数组,而非 Scans 直接拥有 'ScanDetail' 子键。
原始代码中的关键错误有三处:
- ❌ 错误假设:$item['Shipment']['Scans']['ScanDetail'] —— 实际结构是 $item['Shipment']['Scans'][0]['ScanDetail'],即 Scans 是数组,需先遍历;
- ❌ 缺少空值防护:未校验 $item['Shipment'] 或 $item['Shipment']['Scans'] 是否存在,导致 Undefined array key 警告;
- ❌ 表格结构错位:内层 echo '</tr>' 放在最内层循环外,导致 <tr> 开闭不匹配,HTML 渲染异常。
✅ 正确做法是:逐层解构 + 显式判空 + 合理嵌套循环。以下是推荐的健壮实现:
<?php
$json = file_get_contents('https://track.delhivery.com/api/v1/packages/json/?waybill=<THEWAYBILLNUMBER>&token=<THETOKENKEY>');
$arr = json_decode($json, true);
// 安全提取:检查顶层结构是否存在
if (!isset($arr['ShipmentData']) || !is_array($arr['ShipmentData'])) {
echo '<p>⚠️ 未获取到有效运单数据,请检查运单号或 Token。</p>';
exit;
}
$shipments = $arr['ShipmentData'];
echo '<table border="1" class="shipment-table" style="border-collapse: collapse; width: 100%; margin: 1em 0;">';
echo '<thead><tr><th>扫描时间</th><th>操作状态</th><th>扫描类型</th><th>地点</th></tr></thead>';
echo '<tbody>';
foreach ($shipments as $shipment) {
// 确保 Shipment 存在且为数组
if (!isset($shipment['Shipment']) || !is_array($shipment['Shipment'])) {
continue;
}
$shipmentData = $shipment['Shipment'];
// 确保 Scans 存在且为非空数组
$scans = $shipmentData['Scans'] ?? [];
if (!is_array($scans) || empty($scans)) {
continue;
}
// 遍历 Scans 数组(每个元素是 [ 'ScanDetail' => [...] ])
foreach ($scans as $scan) {
// 每个 scan 元素必须包含 ScanDetail 键
if (!isset($scan['ScanDetail']) || !is_array($scan['ScanDetail'])) {
continue;
}
$detail = $scan['ScanDetail'];
// 输出一行扫描记录
echo '<tr>';
echo '<td>' . htmlspecialchars($detail['ScanDateTime'] ?? 'N/A') . '</td>';
echo '<td>' . htmlspecialchars($detail['Scan'] ?? 'N/A') . '</td>';
echo '<td>' . htmlspecialchars($detail['ScanType'] ?? 'N/A') . '</td>';
echo '<td>' . htmlspecialchars($detail['ScannedLocation'] ?? 'N/A') . '</td>';
echo '</tr>';
}
}
echo '</tbody></table>';
?>? 关键要点总结:
立即学习“PHP免费学习笔记(深入)”;
- ✅ 使用 ?? 空合并运算符和 isset() + is_array() 组合进行防御性编程,杜绝未定义键警告;
- ✅ Scans 是数字索引数组([0] => [...], [1] => [...]),必须用 foreach ($scans as $scan) 进入每一项,再取 $scan['ScanDetail'];
- ✅ 所有用户数据输出前务必使用 htmlspecialchars() 防止 XSS;
- ✅ <tr> 必须与 <td> 成对出现在同一层级循环内(即每个扫描记录对应一个 <tr>);
- ✅ 建议添加 <thead> 和 <tbody> 提升语义化与可维护性。
通过以上结构化遍历,即可稳定、安全地将深层嵌套的物流轨迹数据渲染为清晰可读的 HTML 表格。











