使用PHP将嵌套的JSON从URL响应转换为平面JSON

时间:2018-08-04 20:33:54

标签: php arrays json

我要实现的目标是直接动态地平铺JSON嵌套响应。我从API URL得到的响应是

 {   "currency" : "USD",   "results" : [ {
 "itineraries" : [ {
   "outbound" : {
     "duration" : "17:50",
     "flights" : [ {
       "departs_at" : "2018-10-15T08:45",
       "arrives_at" : "2018-10-15T11:00",
       "origin" : {
         "airport" : "IST",
         "terminal" : "I"
       },
       "destination" : {
        "airport" : "LHR",
        "terminal" : "5"
      },
      "marketing_airline" : "BA",
      "operating_airline" : "BA",
      "flight_number" : "675",
      "aircraft" : "320",
      "booking_info" : {
        "travel_class" : "ECONOMY",
        "booking_code" : "S",
        "seats_remaining" : 7
      }
    }, {
      "departs_at" : "2018-10-15T16:55",
      "arrives_at" : "2018-10-15T19:35",
      "origin" : {
        "airport" : "LHR",
        "terminal" : "5"
      },
      "destination" : {
        "airport" : "BOS",
        "terminal" : "E"
      },
      "marketing_airline" : "BA",
      "operating_airline" : "BA",
      "flight_number" : "203",
      "aircraft" : "777",
      "booking_info" : {
        "travel_class" : "ECONOMY",
        "booking_code" : "N",
        "seats_remaining" : 9
      }
    } ]
  },
  "inbound" : {
    "duration" : "14:00",
    "flights" : [ {
      "departs_at" : "2018-10-21T19:25",
      "arrives_at" : "2018-10-22T07:00",
      "origin" : {
        "airport" : "BOS",
        "terminal" : "E"
      },
      "destination" : {
        "airport" : "LHR",
        "terminal" : "5"
      },
      "marketing_airline" : "BA",
      "operating_airline" : "BA",
      "flight_number" : "212",
      "aircraft" : "388",
      "booking_info" : {
        "travel_class" : "ECONOMY",
        "booking_code" : "N",
        "seats_remaining" : 9
      }
    }, {
      "departs_at" : "2018-10-22T10:25",
      "arrives_at" : "2018-10-22T16:25",
      "origin" : {
        "airport" : "LHR",
        "terminal" : "5"
      },
      "destination" : {
        "airport" : "IST",
        "terminal" : "I"
      },
      "marketing_airline" : "BA",
      "operating_airline" : "BA",
      "flight_number" : "676",
      "aircraft" : "320",
      "booking_info" : {
        "travel_class" : "ECONOMY",
        "booking_code" : "S",
        "seats_remaining" : 9
      }
    } ]
  }
} ],
"fare" : {
  "total_price" : "486.17",
  "price_per_adult" : {
    "total_fare" : "486.17",
    "tax" : "343.17"
  },
  "restrictions" : {
    "refundable" : false,
    "change_penalties" : true
  }
}   }, {
"itineraries" : [ {
  "outbound" : {
    "duration" : "10:55",
    "flights" : [ {
      "departs_at" : "2018-10-15T14:35",
      "arrives_at" : "2018-10-15T18:30",
      "origin" : {
        "airport" : "IST",
        "terminal" : "I"
      },
      "destination" : {
        "airport" : "BOS",
        "terminal" : "E"
      },
      "marketing_airline" : "TK",
      "operating_airline" : "TK",
      "flight_number" : "81",
      "aircraft" : "333",
      "booking_info" : {
        "travel_class" : "ECONOMY",
        "booking_code" : "W",
        "seats_remaining" : 9
      }
    } ]
  },
  "inbound" : {
    "duration" : "09:35",
    "flights" : [ {
      "departs_at" : "2018-10-21T23:35",
      "arrives_at" : "2018-10-22T16:10",
      "origin" : {
        "airport" : "BOS",
        "terminal" : "E"
      },
      "destination" : {
        "airport" : "IST",
        "terminal" : "I"
      },
      "marketing_airline" : "TK",
      "operating_airline" : "TK",
      "flight_number" : "82",
      "aircraft" : "333",
      "booking_info" : {
        "travel_class" : "ECONOMY",
        "booking_code" : "W",
        "seats_remaining" : 9
      }
    } ]
  }
} ],
"fare" : {
  "total_price" : "528.31",
  "price_per_adult" : {
    "total_fare" : "528.31",
    "tax" : "344.31"
  },
  "restrictions" : {
    "refundable" : false,
    "change_penalties" : true
  }
}   } ] }

我基本上需要的是将其格式化为扁平格式,因此所有内容都处于同一级别。

我正在得到这样的响应file_get_content $ json_url = file_get_contents('https:// domain / api'); 而且,如果我将其打印出来,效果很好,但是在将结果简化为平坦的JSON响应时遇到了麻烦。

编辑

 {
    "currency": "USD",
    "results[0].itineraries[0].outbound.duration": "17:50",
    "results[0].itineraries[0].outbound.flights[0].departs_at": "2018-10-15T08:45",
    "results[0].itineraries[0].outbound.flights[0].arrives_at": "2018-10-15T11:00",
    "results[0].itineraries[0].outbound.flights[0].origin.airport": "IST",
    "results[0].itineraries[0].outbound.flights[0].origin.terminal": "I",
    "results[0].itineraries[0].outbound.flights[0].destination.airport": "LHR",
    "results[0].itineraries[0].outbound.flights[0].destination.terminal": "5",
    "results[0].itineraries[0].outbound.flights[0].marketing_airline": "BA",
    "results[0].itineraries[0].outbound.flights[0].operating_airline": "BA",
    "results[0].itineraries[0].outbound.flights[0].flight_number": "675",
    "results[0].itineraries[0].outbound.flights[0].aircraft": "320",
    "results[0].itineraries[0].outbound.flights[0].booking_info.travel_class": "ECONOMY",
    "results[0].itineraries[0].outbound.flights[0].booking_info.booking_code": "S",
    "results[0].itineraries[0].outbound.flights[0].booking_info.seats_remaining": 7,
    "results[0].itineraries[0].outbound.flights[1].departs_at": "2018-10-15T16:55",
    "results[0].itineraries[0].outbound.flights[1].arrives_at": "2018-10-15T19:35",
    "results[0].itineraries[0].outbound.flights[1].origin.airport": "LHR",
    "results[0].itineraries[0].outbound.flights[1].origin.terminal": "5",
    "results[0].itineraries[0].outbound.flights[1].destination.airport": "BOS",
    "results[0].itineraries[0].outbound.flights[1].destination.terminal": "E",
    "results[0].itineraries[0].outbound.flights[1].marketing_airline": "BA",
    "results[0].itineraries[0].outbound.flights[1].operating_airline": "BA",
    "results[0].itineraries[0].outbound.flights[1].flight_number": "203",
    "results[0].itineraries[0].outbound.flights[1].aircraft": "777",
    "results[0].itineraries[0].outbound.flights[1].booking_info.travel_class": "ECONOMY",
    "results[0].itineraries[0].outbound.flights[1].booking_info.booking_code": "N",
    "results[0].itineraries[0].outbound.flights[1].booking_info.seats_remaining": 9,
    "results[0].itineraries[0].inbound.duration": "14:00",
    "results[0].itineraries[0].inbound.flights[0].departs_at": "2018-10-21T19:25",
    "results[0].itineraries[0].inbound.flights[0].arrives_at": "2018-10-22T07:00",
    "results[0].itineraries[0].inbound.flights[0].origin.airport": "BOS",
    "results[0].itineraries[0].inbound.flights[0].origin.terminal": "E",
    "results[0].itineraries[0].inbound.flights[0].destination.airport": "LHR",
    "results[0].itineraries[0].inbound.flights[0].destination.terminal": "5",
    "results[0].itineraries[0].inbound.flights[0].marketing_airline": "BA",
    "results[0].itineraries[0].inbound.flights[0].operating_airline": "BA",
    "results[0].itineraries[0].inbound.flights[0].flight_number": "212",
    "results[0].itineraries[0].inbound.flights[0].aircraft": "388",
    "results[0].itineraries[0].inbound.flights[0].booking_info.travel_class": "ECONOMY",
    "results[0].itineraries[0].inbound.flights[0].booking_info.booking_code": "N",
    "results[0].itineraries[0].inbound.flights[0].booking_info.seats_remaining": 9,
    "results[0].itineraries[0].inbound.flights[1].departs_at": "2018-10-22T10:25",
    "results[0].itineraries[0].inbound.flights[1].arrives_at": "2018-10-22T16:25",
    "results[0].itineraries[0].inbound.flights[1].origin.airport": "LHR",
    "results[0].itineraries[0].inbound.flights[1].origin.terminal": "5",
    "results[0].itineraries[0].inbound.flights[1].destination.airport": "IST",
    "results[0].itineraries[0].inbound.flights[1].destination.terminal": "I",
    "results[0].itineraries[0].inbound.flights[1].marketing_airline": "BA",
    "results[0].itineraries[0].inbound.flights[1].operating_airline": "BA",
    "results[0].itineraries[0].inbound.flights[1].flight_number": "676",
    "results[0].itineraries[0].inbound.flights[1].aircraft": "320",
    "results[0].itineraries[0].inbound.flights[1].booking_info.travel_class": "ECONOMY",
    "results[0].itineraries[0].inbound.flights[1].booking_info.booking_code": "S",
    "results[0].itineraries[0].inbound.flights[1].booking_info.seats_remaining": 9,
    "results[0].fare.total_price": "486.17",
    "results[0].fare.price_per_adult.total_fare": "486.17",
    "results[0].fare.price_per_adult.tax": "343.17",
    "results[0].fare.restrictions.refundable": false,
    "results[0].fare.restrictions.change_penalties": true
    }
    {
    "currency": "USD",
    "results[1].itineraries[0].outbound.duration": "10:55",
    "results[1].itineraries[0].outbound.flights[0].departs_at": "2018-10-15T14:35",
    "results[1].itineraries[0].outbound.flights[0].arrives_at": "2018-10-15T18:30",
    "results[1].itineraries[0].outbound.flights[0].origin.airport": "IST",
    "results[1].itineraries[0].outbound.flights[0].origin.terminal": "I",
    "results[1].itineraries[0].outbound.flights[0].destination.airport": "BOS",
    "results[1].itineraries[0].outbound.flights[0].destination.terminal": "E",
    "results[1].itineraries[0].outbound.flights[0].marketing_airline": "TK",
    "results[1].itineraries[0].outbound.flights[0].operating_airline": "TK",
    "results[1].itineraries[0].outbound.flights[0].flight_number": "81",
    "results[1].itineraries[0].outbound.flights[0].aircraft": "333",
    "results[1].itineraries[0].outbound.flights[0].booking_info.travel_class": "ECONOMY",
    "results[1].itineraries[0].outbound.flights[0].booking_info.booking_code": "W",
    "results[1].itineraries[0].outbound.flights[0].booking_info.seats_remaining": 9,
    "results[1].itineraries[0].inbound.duration": "09:35",
    "results[1].itineraries[0].inbound.flights[0].departs_at": "2018-10-21T23:35",
    "results[1].itineraries[0].inbound.flights[0].arrives_at": "2018-10-22T16:10",
    "results[1].itineraries[0].inbound.flights[0].origin.airport": "BOS",
    "results[1].itineraries[0].inbound.flights[0].origin.terminal": "E",
    "results[1].itineraries[0].inbound.flights[0].destination.airport": "IST",
    "results[1].itineraries[0].inbound.flights[0].destination.terminal": "I",
    "results[1].itineraries[0].inbound.flights[0].marketing_airline": "TK",
    "results[1].itineraries[0].inbound.flights[0].operating_airline": "TK",
    "results[1].itineraries[0].inbound.flights[0].flight_number": "82",
    "results[1].itineraries[0].inbound.flights[0].aircraft": "333",
    "results[1].itineraries[0].inbound.flights[0].booking_info.travel_class": "ECONOMY",
    "results[1].itineraries[0].inbound.flights[0].booking_info.booking_code": "W",
    "results[1].itineraries[0].inbound.flights[0].booking_info.seats_remaining": 9,
    "results[1].fare.total_price": "528.31",
    "results[1].fare.price_per_adult.total_fare": "528.31",
    "results[1].fare.price_per_adult.tax": "344.31",
    "results[1].fare.restrictions.refundable": false,
    "results[1].fare.restrictions.change_penalties": true
}

谢谢

1 个答案:

答案 0 :(得分:1)

这是一种复制您期望的json格式的方法。
我已经从This answer借了一些代码,并稍作调整以使其适合您,请记住给它一个赞。

我使用注释中的代码创建数组的货币部分。
然后,我将每个新的子数组部分传递给代码,该代码回溯路径并使它成为json代码。

$arr = json_decode($str,true);  // $str is the json
foreach($arr['results'] as $key => $res){
    $new[$key]['currency'] = $arr['currency'];
    $new[$key]['results'][] = $res;
}
foreach($new as $n){
    $json[] = listArrayRecursive($n);
}
$jsonNew = "[" . implode(",", $json) . "]"; // this is the new json formed as you want it
var_dump(json_decode($jsonNew), true); // for debug purpose test it




function listArrayRecursive($someArray) {
    $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($someArray), RecursiveIteratorIterator::SELF_FIRST);
    $str = "{";
    foreach ($iterator as $k => $v) {

        // Not at end: show key only
        if ($iterator->hasChildren()) {

        } else {
            for ($p = array(), $i = 0, $z = $iterator->getDepth(); $i <= $z; $i++) {
                $p[] = $iterator->getSubIterator($i)->key();
            }
            $path = implode('.', $p);
            $str .= '"' . $path . '": "' .$v . '"' . ",";
        }
    }
    $str = substr($str,0,-1); // remove trailing comma that makes json invalid
    $str .= "}\n";
    return $str;
}

https://3v4l.org/eRCIY