You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP中如何从多层嵌套JSON数组提取DELIVERED特定值?

从JSON转成的关联数组中提取含"DELIVERED"的内容

问题背景

已通过$results = json_decode($response, true);将JSON响应转为关联数组,尝试循环遍历查找键值对但未成功,需要提取数组中包含“DELIVERED”的内容。

对应的数组结构如下:

array(1) {
  ["trackResponse"]=>
  array(1) {
    ["shipment"]=>
    array(1) {
      [0]=>
      array(3) {
        ["inquiryNumber"]=>
        string(18) "MyID_Number"
        ["package"]=>
        array(1) {
          [0]=>
          array(12) {
            ["trackingNumber"]=>
            string(18) "MyID_Number"
            ["deliveryDate"]=>
            array(1) {
              [0]=>
              array(2) {
                ["type"]=>
                string(3) "DEL"
                ["date"]=>
                string(8) "20240403"
              }
            }
            ["deliveryTime"]=>
            array(2) {
              ["type"]=>
              string(3) "DEL"
              ["endTime"]=>
              string(6) "162314"
            }
            ["activity"]=>
            array(8) {
              [0]=>
              array(7) {
                ["location"]=>
                array(2) {
                  ["address"]=>
                  array(4) {
                    ["city"]=>
                    string(7) "BRAXTON"
                    ["stateProvince"]=>
                    string(2) "MS"
                    ["countryCode"]=>
                    string(2) "US"
                    ["country"]=>
                    string(2) "US"
                  }
                  ["slic"]=>
                  string(4) "3921"
                }
                ["status"]=>
                array(4) {
                  ["type"]=>
                  string(1) "D"
                  ["description"]=>
                  string(10) "DELIVERED "
                  ["code"]=>
                  string(2) "FS"
                  ["statusCode"]=>
                  string(3) "011"
                }
                ["date"]=>
                string(8) "20240403"
                ["time"]=>
                string(6) "162314"
                ["gmtDate"]=>
                string(8) "20240403"
                ["gmtOffset"]=>
                string(6) "-05:00"
                ["gmtTime"]=>
                string(8) "21:23:14"
              }
              [1]=>
              array(7) {
                ["location"]=>
                array(2) {
                  ["address"]=>
                  array(4) {
                    ["city"]=>
                    string(7) "Jackson"
                    ["stateProvince"]=>
                    string(2) "MS"
                    ["countryCode"]=>
                    string(2) "US"
                    ["country"]=>
                    string(2) "US"
                  }
                  ["slic"]=>
                  string(4) "3921"
                }
                ["status"]=>
                array(4) {
                  ["type"]=>
                  string(1) "I"
                  ["description"]=>
                  string(22) "Out For Delivery Today"
                  ["code"]=>
                  string(2) "OT"
                  ["statusCode"]=>
                  string(3) "021"
                }
                ["date"]=>
                string(8) "20240403"
                ["time"]=>
                string(6) "091057"
                ["gmtDate"]=>
                string(8) "20240403"
                ["gmtOffset"]=>
                string(6) "-05:00"
                ["gmtTime"]=>
                string(8) "14:10:57"
              }
            }
          }
        }
      }
    }
  }
}

可行提取方法

方法1:直接定位数组层级(结构固定时使用)

根据给定的数组结构,DELIVERED存在于activity数组的子项中,直接遍历该数组过滤目标项:

// 定位到activity数组
$activities = $results['trackResponse']['shipment'][0]['package'][0]['activity'];

// 过滤出包含DELIVERED的活动项
$deliveredItems = array_filter($activities, function($item) {
    // 去除字符串前后空格后判断,适配原数据中"DELIVERED "带空格的情况
    return str_contains(trim($item['status']['description']), 'DELIVERED');
});

// 输出结果
print_r($deliveredItems);

方法2:递归遍历(结构可能变化时使用)

如果数组层级不固定,用递归遍历所有键值对,提取包含目标内容的项:

function findDeliveredItems($array, &$result = []) {
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            findDeliveredItems($value, $result);
        } else {
            // 检查当前字符串是否包含DELIVERED
            if (is_string($value) && str_contains(trim($value), 'DELIVERED')) {
                $result[] = $array;
                break;
            }
        }
    }
    return $result;
}

// 调用函数获取结果
$deliveredItems = findDeliveredItems($results);
print_r($deliveredItems);

注意事项

  • 原数据中DELIVERED后带有空格,使用trim()处理后再判断可避免匹配失败
  • 方法1效率更高,适合结构固定的场景;方法2更灵活,适配数组结构可能变化的情况

内容的提问来源于stack exchange,提问作者user2843577

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 07:45:54