使用PHP,如何找到[departs_at]字段的出现次数?

时间:2018-07-05 12:06:04

标签: php json

for i in range(0,len(df2['Keyword'])):
    for j in range(0,len(df1['job'])):
        if df1[df1['job'][j].str.contains(df2['Keyword'].values[i],na=False)]:
            print(df1.ix[j,'Name'],df1.ix[j,'Age'])

这是从实时航班查询中获取的json数据。我希望能够使用[depart_at]字段来确定每次航班的停靠站数量。因此,我一直在努力获取[depart_at]字段的出现次数,以便得出每个航班的停靠点数(停留时间)。我已经使用以下代码转换为php:但是我没有得到计数。

{
  "currency": "USD",
  "results": [
{
  "itineraries": [
    {
      "outbound": {
        "duration": "08:35",
        "flights": [
          {
            "departs_at": "2018-07-03T18:35",
            "arrives_at": "2018-07-03T21:15",
            "origin": {
              "airport": "BOS",
              "terminal": "A"
            },
            "destination": {
              "airport": "YHZ"
            },
            "marketing_airline": "WS",
            "operating_airline": "WS",
            "flight_number": "3713",
            "aircraft": "DH4",
            "booking_info": {
              "travel_class": "ECONOMY",
              "booking_code": "M",
              "seats_remaining": 5
            }
          },
          {
            "departs_at": "2018-07-03T22:20",
            "arrives_at": "2018-07-04T08:10",
            "origin": {
              "airport": "YHZ"
            },
            "destination": {
              "airport": "LGW",
              "terminal": "N"
            },
            "marketing_airline": "WS",
            "operating_airline": "WS",
            "flight_number": "24",
            "aircraft": "7M8",
            "booking_info": {
              "travel_class": "ECONOMY",
              "booking_code": "M",
              "seats_remaining": 1
            }
          }
        ]
      }
    }
  ],
  "fare": {
    "total_price": "653.40",
    "price_per_adult": {
      "total_fare": "653.40",
      "tax": "178.40"
    },
    "restrictions": {
      "refundable": true,
      "change_penalties": true
    }
  }
},

2 个答案:

答案 0 :(得分:0)

您可以尝试这样:

        $json = '{
      "currency": "USD",
      "results": [
    {
      "itineraries": [
        {
          "outbound": {
            "duration": "08:35",
            "flights": [
              {
                "departs_at": "2018-07-03T18:35",
                "arrives_at": "2018-07-03T21:15",
                "origin": {
                  "airport": "BOS",
                  "terminal": "A"
                },
                "destination": {
                  "airport": "YHZ"
                },
                "marketing_airline": "WS",
                "operating_airline": "WS",
                "flight_number": "3713",
                "aircraft": "DH4",
                "booking_info": {
                  "travel_class": "ECONOMY",
                  "booking_code": "M",
                  "seats_remaining": 5
                }
              },
              {
                "departs_at": "2018-07-03T22:20",
                "arrives_at": "2018-07-04T08:10",
                "origin": {
                  "airport": "YHZ"
                },
                "destination": {
                  "airport": "LGW",
                  "terminal": "N"
                },
                "marketing_airline": "WS",
                "operating_airline": "WS",
                "flight_number": "24",
                "aircraft": "7M8",
                "booking_info": {
                  "travel_class": "ECONOMY",
                  "booking_code": "M",
                  "seats_remaining": 1
                }
              }
            ]
          }
        }
      ],
      "fare": {
        "total_price": "653.40",
        "price_per_adult": {
          "total_fare": "653.40",
          "tax": "178.40"
        },
        "restrictions": {
          "refundable": true,
          "change_penalties": true
        }
      }
    }';

    echo substr_count($json, 'departs_at');

答案 1 :(得分:0)

您可以尝试这种方式。

$countDepartsAt = 0;
foreach ($itinerary['outbound']['flights'] as $flight ){
    if(array_key_exists('departs_at', $flight)) {
        $countDepartsAt++;
    }
}

echo $countDepartsAt;