Parsing JSON Url with an Array Response - php

I'm running this code in php:
$jsonData = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address=306%20Evarts%20St%20NE%20Washington,%20DC%2020002&key=<hidden>');
$data = json_decode($jsonData, true);
var_dump($data);
with the following result (reformatted to be readable):
array(2) {
["results"] => array(1) {
[0] => array(5) {
["address_components"] => array(7) {
[0] => array(3) {
["long_name"] => string(3) "306"
["short_name"]=> string(3) "306"
["types"] => array(1) {
[0]=> string(13) "street_number"
}
}
[1] => array(3) {
["long_name"] => string(23) "Evarts Street Northeast"
["short_name"] => string(12) "Evarts St NE"
["types"] => array(1) {
[0]=> string(5) "route"
}
}
[2] => array(3) {
["long_name"] => string(20) "Northeast Washington"
["short_name"] => string(20) "Northeast Washington"
["types"] => array(2) {
[0] => string(12) "neighborhood"
[1] => string(9) "political"
}
}
[3] => array(3) {
["long_name"] => string(10) "Washington"
["short_name"]=> string(4) "D.C."
["types"]=> array(2) {
[0]=> string(8) "locality"
[1]=> string(9) "political"
}
}
[4]=> array(3) {
["long_name"]=> string(20) "District of Columbia"
["short_name"]=> string(2) "DC"
["types"]=> array(2) {
[0]=> string(27) "administrative_area_level_1"
[1]=> string(9) "political"
}
}
[5]=> array(3) {
["long_name"]=> string(13) "United States"
["short_name"]=> string(2) "US"
["types"]=> array(2) {
[0]=> string(7) "country"
[1]=> string(9) "political"
}
}
[6]=> array(3) {
["long_name"]=> string(5) "20002"
["short_name"]=> string(5) "20002"
["types"]=> array(1) {
[0]=> string(11) "postal_code"
}
}
}
["formatted_address"]=> string(54) "306 Evarts Street Northeast, Washington, DC 20002, USA"
["geometry"]=> array(3) {
["location"]=> array(2) {
["lat"]=> float(38.9248059)
["lng"]=> float(-77.001318)
}
["location_type"]=> string(7) "ROOFTOP"
["viewport"]=> array(2) {
["northeast"]=> array(2) {
["lat"]=> float(38.926154880291)
["lng"]=> float(-76.999969019709)
}
["southwest"]=> array(2) {
["lat"]=> float(38.923456919708)
["lng"]=> float(-77.002666980292)
}
}
}
["place_id"]=> string(27) "ChIJvysMWvjHt4kRjif4nyaEb7U"
["types"]=> array(1) {
[0]=> string(14) "street_address"
}
}
}
["status"]=> string(2) "OK"
}
How can I echo various values in the results for example long_name of address components?

you can use nested foreach to loop array ref : http://php.net/manual/en/control-structures.foreach.php
foreach($data['results'] as $result){
foreach ($result['address_components'] as $value) {
echo $value['long_name'] . '<br>';
}
}

You can access response data as shown below:
$jsonData = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address=306%20Evarts%20St%20NE%20Washington,%20DC%2020002&key=API_KEY');
$data = json_decode($jsonData, true);
if($data['status'] == 'OK'){
$latitude = $data['results'][0]['geometry']['location']['lat'];
$longitude = $data['results'][0]['geometry']['location']['lng'];
$formatted_address = $data['results'][0]['formatted_address'];
}

Related

PHP Convert multidimensional array key to be array multidimensional value

I have problem formatting array to become fancytree format, below array output from db:
array(44) {
[0]=>
array(3) {
["id"]=>
string(2) "35"
["title"]=>
string(28) "ROOT1"
["path"]=>
string(28) "ROOT1"
}
[1]=>
array(3) {
["id"]=>
string(2) "36"
["title"]=>
string(27) "SUB_A"
["path"]=>
string(56) "ROOT1>SUB_A"
}
[2]=>
array(3) {
["id"]=>
string(2) "39"
["title"]=>
string(12) "SUB_A_1"
["path"]=>
string(69) "ROOT1>SUB_A>SUB_A_1"
}
[3]=>
array(3) {
["id"]=>
string(2) "37"
["title"]=>
string(28) "SUB_A_2"
["path"]=>
string(85) "ROOT1>SUB_A>SUB_A_2"
}
[4]=>
array(3) {
["id"]=>
string(2) "40"
["title"]=>
string(30) "SUB_A_3"
["path"]=>
string(87) "ROOT1>SUB_A>SUB_A_3"
}
[5]=>
array(3) {
["id"]=>
string(2) "43"
["title"]=>
string(19) "SUB_A_4"
["path"]=>
string(76) "ROOT1>SUB_A>SUB_A_4"
}
[6]=>
array(3) {
["id"]=>
string(2) "41"
["title"]=>
string(12) "SUB_A_5"
["path"]=>
string(69) "ROOT1>SUB_A>SUB_A_5"
}
[7]=>
array(3) {
["id"]=>
string(1) "1"
["title"]=>
string(20) "ROOT2"
["path"]=>
string(20) "ROOT2"
}
[8]=>
array(3) {
["id"]=>
string(2) "30"
["title"]=>
string(37) "ROOT3"
["path"]=>
string(37) "ROOT3"
}
[9]=>
array(3) {
["id"]=>
string(2) "34"
["title"]=>
string(21) "SUB_B"
["path"]=>
string(59) "ROOT3>SUB_B"
}
[10]=>
array(3) {
["id"]=>
string(2) "31"
["title"]=>
string(15) "SUB_C"
["path"]=>
string(53) "ROOT3>SUB_C"
}
I want to format values of path become structured array, here is what I try:
function format(array $data) : array
{
$single = [];
// format to single data
foreach ($data as $value) {
$single[] = $value['path'];
}
$result = [];
foreach ($single as $path) {
$parts = explode('>', $path);
$section = &$result;
$sectionName = '';
foreach ($parts as $part) {
$sectionName = $part;
if (array_key_exists($sectionName, $section) === FALSE) {
$section[$sectionName] = [];
}
$section = &$section[$sectionName];
}
}
return $result;
}
result:
array(3) {
["ROOT1"]=>
array(1) {
["SUB_A"]=>
array(5) {
["SUB_A_1"]=>
array(0) {
}
["SUB_A_2"]=>
array(0) {
}
["SUB_A_3"]=>
array(0) {
}
["SUB_A_4"]=>
array(0) {
}
["SUB_A_5"]=>
array(0) {
}
}
}
["ROOT2"]=>
array(0) {
}
["ROOT3"]=>
array(2) {
["SUB_B"]=>
array(0) {
}
["SUB_C"]=>
array(0) {
}
}
i need to include values of id and title to each path as expected result:
array(3) {
[0]=>
array(3) {
["title"]=>
string(5) "ROOT1"
["id"]=>
int(35)
["children"]=>
array(1) {
[0]=>
array(3) {
["title"]=>
string(5) "SUB_A"
["id"]=>
int(36)
["children"]=>
array(5) {
[0]=>
array(2) {
["title"]=>
string(7) "SUB_A_1"
["id"]=>
int(39)
}
[1]=>
array(2) {
["title"]=>
string(7) "SUB_A_2"
["id"]=>
int(37)
}
[2]=>
array(2) {
["title"]=>
string(7) "SUB_A_3"
["id"]=>
int(40)
}
[3]=>
array(2) {
["title"]=>
string(7) "SUB_A_4"
["id"]=>
int(43)
}
[4]=>
array(2) {
["title"]=>
string(7) "SUB_A_5"
["id"]=>
int(41)
}
}
}
}
}
[1]=>
array(2) {
["title"]=>
string(5) "ROOT2"
["id"]=>
int(1)
}
[2]=>
array(3) {
["title"]=>
string(5) "ROOT3"
["id"]=>
int(30)
["children"]=>
array(2) {
[0]=>
array(2) {
["title"]=>
string(5) "SUB_B"
["id"]=>
int(34)
}
[1]=>
array(2) {
["title"]=>
string(5) "SUB_C"
["id"]=>
int(31)
}
}
}
}
I would be grateful for any help you are able to provide.
Here's how I would do it. Test
I used arrow functions to reduce the amount of lines and there are inline comments to explain what I did.
<?php
$data = [["id"=> "35", "title"=> "ROOT1", "path"=> "ROOT1"], ["id"=> "36", "title"=> "SUB_A", "path"=> "ROOT1>SUB_A"], ["id"=> "37", "title"=> "SUB_A_1", "path"=> "ROOT1>SUB_A>SUB_A_1"], ["id"=> "38", "title"=> "SUB_A_2", "path"=> "ROOT1>SUB_A>SUB_A_2"], ["id"=> "39", "title"=> "SUB_A_3", "path"=> "ROOT1>SUB_A>SUB_A_3"], ["id"=> "40", "title"=> "SUB_A_4", "path"=> "ROOT1>SUB_A>SUB_A_4"], ["id"=> "41", "title"=> "SUB_A_5", "path"=> "ROOT1>SUB_A>SUB_A_5"], ["id"=> "1", "title"=> "ROOT2", "path"=> "ROOT2"], ["id"=> "30", "title"=> "ROOT3", "path"=> "ROOT3"], ["id"=> "34", "title"=> "SUB_B", "path"=> "ROOT3>SUB_B"], ["id"=> "31", "title"=> "SUB_C", "path"=> "ROOT3>SUB_C"]];
// The path is defaulted to an empty string, so the regex would return the highest level elements only because they don't have any `>`
function get_children($data, $path = '')
{
$results = array_values(array_filter($data, fn($item) => preg_match("/^{$path}[^>]+$/", $item['path'])));
// This regex, uses the passed path (e.g ROOT1) and searches for elements with anything but another `>`, because `>` signifies an inner directory.
// So this would return elements with a path like `ROOT1>SUB_A, ROOT1>SUB_B` but not `ROOT1>SUB_A>SUB_A_1`
foreach($results as $i => $result) {
// then I iterate through the result to get the element's children
$children = array_values(get_children($data, "{$result['path']}>"));
if (count($children)) $results[$i]['children'] = $children;
unset($results[$i]['path']); // Removing the path key from the array
}
return $results;
}
$format = fn($data) => get_children($data);
print_r($format($data));

Restructure an array of events in PHP combining attendees with common location

I have a PHP array that holds the dates, locations and time a person will be attending an event. It starts in this format (The original array is always delivered in date/time order):
array(2) {
[0]=>
array(2) {
["date"]=>
string(8) "1st June"
["events"]=>
array(4) {
[0]=>
array(3) {
["location"]=>
string(7) "Venue A"
["name"]=>
string(4) "Pete"
["time"]=>
string(6) "4.00pm"
}
[1]=>
array(3) {
["location"]=>
string(7) "Venue A"
["name"]=>
string(4) "John"
["time"]=>
string(6) "4.30pm"
}
[2]=>
array(3) {
["location"]=>
string(7) "Venue B"
["name"]=>
string(5) "Chris"
["time"]=>
string(6) "7.30pm"
}
[3]=>
array(3) {
["location"]=>
string(7) "Venue A"
["name"]=>
string(4) "Mark"
["time"]=>
string(6) "8.00pm"
}
}
}
[1]=>
array(2) {
["date"]=>
string(8) "2nd June"
["events"]=>
array(4) {
[0]=>
array(3) {
["location"]=>
string(7) "Venue B"
["name"]=>
string(4) "Fred"
["time"]=>
string(6) "5.00pm"
}
[1]=>
array(3) {
["location"]=>
string(7) "Venue C"
["name"]=>
string(5) "Boris"
["time"]=>
string(6) "6.00pm"
}
[2]=>
array(3) {
["location"]=>
string(7) "Venue A"
["name"]=>
string(6) "Rupert"
["time"]=>
string(6) "7.00pm"
}
[3]=>
array(3) {
["location"]=>
string(7) "Venue A"
["name"]=>
string(5) "David"
["time"]=>
string(6) "9.00pm"
}
}
}
}
What I need to do is combine this so attendees at the same location are grouped together where another attendee in a different location doesn't break up the list. So the above array would be changed to this:
array(2) {
[0]=>
array(2) {
["date"]=>
string(8) "1st June"
["events"]=>
array(3) {
[0]=>
array(2) {
["location"]=>
string(7) "Venue A"
["attendees"]=>
array(2) {
[0]=>
array(2) {
["name"]=>
string(4) "Pete"
["time"]=>
string(6) "4.00pm"
}
[1]=>
array(2) {
["name"]=>
string(4) "John"
["time"]=>
string(6) "4.30pm"
}
}
}
[1]=>
array(2) {
["location"]=>
string(7) "Venue B"
["attendees"]=>
array(1) {
[0]=>
array(2) {
["name"]=>
string(5) "Chris"
["time"]=>
string(6) "7.30pm"
}
}
}
[2]=>
array(2) {
["location"]=>
string(7) "Venue A"
["attendees"]=>
array(1) {
[0]=>
array(2) {
["name"]=>
string(4) "Mark"
["time"]=>
string(6) "8.00pm"
}
}
}
}
}
[1]=>
array(2) {
["date"]=>
string(8) "2nd June"
["events"]=>
array(3) {
[0]=>
array(2) {
["location"]=>
string(7) "Venue B"
["attendees"]=>
array(1) {
[0]=>
array(2) {
["name"]=>
string(4) "Fred"
["time"]=>
string(6) "5.00pm"
}
}
}
[1]=>
array(2) {
["location"]=>
string(7) "Venue C"
["attendees"]=>
array(1) {
[0]=>
array(2) {
["name"]=>
string(5) "Boris"
["time"]=>
string(6) "6.00pm"
}
}
}
[2]=>
array(2) {
["location"]=>
string(7) "Venue A"
["attendees"]=>
array(2) {
[0]=>
array(2) {
["name"]=>
string(6) "Rupert"
["time"]=>
string(6) "7.00pm"
}
[1]=>
array(2) {
["name"]=>
string(5) "David"
["time"]=>
string(6) "9.00pm"
}
}
}
}
}
}
So here's how I've figured out to do it juggling variables and stepping through the original array:
// $eventsArray holds the original array data a per the first example
$newArray = array();
foreach($eventsArray as $day) {
$curLocation = '';
$todaysEvents = array();
$teKey = -1;
foreach($day['events'] as $event) {
if($curLocation==$event['location']) {
$todaysEvents[$teKey]['attendees'][] = array(
'name' => $event['name'],
'time' => $event['time']
);
} else {
$todaysEvents[] = array(
'location' => $event['location'],
'attendees' => array(
array(
'name' => $event['name'],
'time' => $event['time']
)
)
);
$teKey = $teKey + 1;
}
$curLocation=$event['location'];
}
$newArray[] = array(
'date' => $day['date'],
'events' => $todaysEvents
);
}
And this works! So why am I asking a question on Stack? Because this feels awfully clunky and I'm not convinced there isn't a much more efficient way of doing it. I get the feeling I'm adding overhead here where it's not necessary.
Here's an alternative version using some PHP 7 (7.3+) features:
$resultEvents = [];
foreach ($events as ['date' => $date, 'events' => $dayEvents]) {
$resultDayEvents = ['date' => $date, 'events' => []];
foreach ($dayEvents as ['location' => $location, 'name' => $name, 'time' => $time]) {
$attendee = ['name' => $name, 'time' => $time];
if ($location === ($previousLocation ?? null)) {
$resultDayEvents['events'][array_key_last($resultDayEvents['events'])]['attendees'][] = $attendee;
} else {
$resultDayEvents['events'][] = ['location' => $location, 'attendees' => [$attendee]];
}
$previousLocation = $location;
}
$resultEvents[] = $resultDayEvents;
}
Demo: https://3v4l.org/bC4LK
You can use references like a cursor to keep track of the last event processed. Less effort to compare location and add attendees. But that comes with the complexity of getting rid of it properly.
Also, but a minor thing is to use strict comparisons... Can avoid some frustrating moments by being more predectible...
// $eventsArray holds the original array data a per the first example
$newArray = array();
foreach($eventsArray as $day) {
$todaysEvents = array();
$curEvent = null;
foreach($day['events'] as $event) {
if($curEvent===null || $curEvent['location']!==$event['location']) {
unset($curEvent);
$curEvent = array(
'location' => $event['location'],
'attendees' => array()
);
$todaysEvents[] = &$curEvent;
}
$curEvent['attendees'][] = array(
'name' => $event['name'],
'time' => $event['time']
);
}
unset($curEvent);
$newArray[] = array(
'date' => $day['date'],
'events' => $todaysEvents
);
}

How to output all flights in HTML with this nested array?

Just want to output the array I get using: https://github.com/dahabtours/amadeus-php-sdk
$date_checkin = '2019-07-16';
$date_checkout = '2019-07-18';
require('AmadeusClient.php');
$amadeus_api = new AmadeusDahabtours\SelfServiceApiClient('CLIENT_ID','CLIENT_SECRET');
# Flight Low-fare Search
$flight_offers = $amadeus_api->lowFares([
'origin' => 'AMS',
'destination' => 'BCN',
'departureDate' => $date_checkin,
'returnDate' => $date_checkout,
'nonStop' => true,
'currency' => 'EUR',
'max' => 2,
]);
I want the output to be something like this:
<div class="flights">
<div class="flight-1">VALUES</div>
<div class="flight-2">VALUES</div>
<div class="flight-3">VALUES</div>
</div>
Array:
array(5) {
["success"]=>
bool(true)
["msgs"]=>
array(0) {
}
["http_code"]=>
int(200)
["response_text"]=>
string(2597) "{"data":[{"type":"flight-offer","id":"1562680375384--1832628087","offerItems":[{"services":[{"segments":[{"flightSegment":{"departure":{"iataCode":"AMS","terminal":"3","at":"2019-07-16T07:00:00+02:00"},"arrival":{"iataCode":"BCN","terminal":"1","at":"2019-07-16T09:20:00+02:00"},"carrierCode":"IB","number":"5224","aircraft":{"code":"320"},"operating":{"carrierCode":"VY","number":"5224"},"duration":"0DT2H20M"},"pricingDetailPerAdult":{"travelClass":"ECONOMY","fareClass":"V","availability":4,"fareBasis":"VRTNVY"}}]},{"segments":[{"flightSegment":{"departure":{"iataCode":"BCN","terminal":"1","at":"2019-07-18T12:05:00+02:00"},"arrival":{"iataCode":"AMS","at":"2019-07-18T14:30:00+02:00"},"carrierCode":"IB","number":"5685","aircraft":{"code":"320"},"operating":{"carrierCode":"VY","number":"5685"},"duration":"0DT2H25M"},"pricingDetailPerAdult":{"travelClass":"ECONOMY","fareClass":"A","availability":4,"fareBasis":"ARTNVY"}}]}],"price":{"total":"209.09","totalTaxes":"74.09"},"pricePerAdult":{"total":"209.09","totalTaxes":"74.09"}}]},{"type":"flight-offer","id":"1562680375384--510820791","offerItems":[{"services":[{"segments":[{"flightSegment":{"departure":{"iataCode":"AMS","at":"2019-07-16T07:00:00+02:00"},"arrival":{"iataCode":"BCN","terminal":"1","at":"2019-07-16T09:20:00+02:00"},"carrierCode":"VY","number":"8318","aircraft":{"code":"320"},"operating":{"carrierCode":"VY","number":"8318"},"duration":"0DT2H20M"},"pricingDetailPerAdult":{"travelClass":"ECONOMY","fareClass":"X","availability":6,"fareBasis":"XRTVY"}}]},{"segments":[{"flightSegment":{"departure":{"iataCode":"BCN","terminal":"1","at":"2019-07-18T12:05:00+02:00"},"arrival":{"iataCode":"AMS","at":"2019-07-18T14:30:00+02:00"},"carrierCode":"VY","number":"8302","aircraft":{"code":"321"},"operating":{"carrierCode":"VY","number":"8302"},"duration":"0DT2H25M"},"pricingDetailPerAdult":{"travelClass":"ECONOMY","fareClass":"P","availability":8,"fareBasis":"PRTVY"}}]}],"price":{"total":"209.09","totalTaxes":"74.09"},"pricePerAdult":{"total":"209.09","totalTaxes":"74.09"}}]}],"dictionaries":{"carriers":{"VY":"VUELING AIRLINES","IB":"IBERIA"},"currencies":{"EUR":"EURO"},"aircraft":{"320":"AIRBUS INDUSTRIE A320-100/200","321":"AIRBUS INDUSTRIE A321"},"locations":{"AMS":{"subType":"AIRPORT","detailedName":"SCHIPHOL AIRPORT"},"BCN":{"subType":"AIRPORT","detailedName":"AIRPORT"}}},"meta":{"links":{"self":"https://test.api.amadeus.com/v1/shopping/flight-offers?origin=AMS&destination=BCN&departureDate=2019-07-16&returnDate=2019-07-18&adults=1&nonStop=true¤cy=EUR&max=2"},"currency":"EUR","defaults":{"adults":1}}}"
["response"]=>
array(3) {
["data"]=>
array(2) {
[0]=>
array(3) {
["type"]=>
string(12) "flight-offer"
["id"]=>
string(25) "1562680375384--1832628087"
["offerItems"]=>
array(1) {
[0]=>
array(3) {
["services"]=>
array(2) {
[0]=>
array(1) {
["segments"]=>
array(1) {
[0]=>
array(2) {
["flightSegment"]=>
array(7) {
["departure"]=>
array(3) {
["iataCode"]=>
string(3) "AMS"
["terminal"]=>
string(1) "3"
["at"]=>
string(25) "2019-07-16T07:00:00+02:00"
}
["arrival"]=>
array(3) {
["iataCode"]=>
string(3) "BCN"
["terminal"]=>
string(1) "1"
["at"]=>
string(25) "2019-07-16T09:20:00+02:00"
}
["carrierCode"]=>
string(2) "IB"
["number"]=>
string(4) "5224"
["aircraft"]=>
array(1) {
["code"]=>
string(3) "320"
}
["operating"]=>
array(2) {
["carrierCode"]=>
string(2) "VY"
["number"]=>
string(4) "5224"
}
["duration"]=>
string(8) "0DT2H20M"
}
["pricingDetailPerAdult"]=>
array(4) {
["travelClass"]=>
string(7) "ECONOMY"
["fareClass"]=>
string(1) "V"
["availability"]=>
int(4)
["fareBasis"]=>
string(6) "VRTNVY"
}
}
}
}
[1]=>
array(1) {
["segments"]=>
array(1) {
[0]=>
array(2) {
["flightSegment"]=>
array(7) {
["departure"]=>
array(3) {
["iataCode"]=>
string(3) "BCN"
["terminal"]=>
string(1) "1"
["at"]=>
string(25) "2019-07-18T12:05:00+02:00"
}
["arrival"]=>
array(2) {
["iataCode"]=>
string(3) "AMS"
["at"]=>
string(25) "2019-07-18T14:30:00+02:00"
}
["carrierCode"]=>
string(2) "IB"
["number"]=>
string(4) "5685"
["aircraft"]=>
array(1) {
["code"]=>
string(3) "320"
}
["operating"]=>
array(2) {
["carrierCode"]=>
string(2) "VY"
["number"]=>
string(4) "5685"
}
["duration"]=>
string(8) "0DT2H25M"
}
["pricingDetailPerAdult"]=>
array(4) {
["travelClass"]=>
string(7) "ECONOMY"
["fareClass"]=>
string(1) "A"
["availability"]=>
int(4)
["fareBasis"]=>
string(6) "ARTNVY"
}
}
}
}
}
["price"]=>
array(2) {
["total"]=>
string(6) "209.09"
["totalTaxes"]=>
string(5) "74.09"
}
["pricePerAdult"]=>
array(2) {
["total"]=>
string(6) "209.09"
["totalTaxes"]=>
string(5) "74.09"
}
}
}
}
[1]=>
array(3) {
["type"]=>
string(12) "flight-offer"
["id"]=>
string(24) "1562680375384--510820791"
["offerItems"]=>
array(1) {
[0]=>
array(3) {
["services"]=>
array(2) {
[0]=>
array(1) {
["segments"]=>
array(1) {
[0]=>
array(2) {
["flightSegment"]=>
array(7) {
["departure"]=>
array(2) {
["iataCode"]=>
string(3) "AMS"
["at"]=>
string(25) "2019-07-16T07:00:00+02:00"
}
["arrival"]=>
array(3) {
["iataCode"]=>
string(3) "BCN"
["terminal"]=>
string(1) "1"
["at"]=>
string(25) "2019-07-16T09:20:00+02:00"
}
["carrierCode"]=>
string(2) "VY"
["number"]=>
string(4) "8318"
["aircraft"]=>
array(1) {
["code"]=>
string(3) "320"
}
["operating"]=>
array(2) {
["carrierCode"]=>
string(2) "VY"
["number"]=>
string(4) "8318"
}
["duration"]=>
string(8) "0DT2H20M"
}
["pricingDetailPerAdult"]=>
array(4) {
["travelClass"]=>
string(7) "ECONOMY"
["fareClass"]=>
string(1) "X"
["availability"]=>
int(6)
["fareBasis"]=>
string(5) "XRTVY"
}
}
}
}
[1]=>
array(1) {
["segments"]=>
array(1) {
[0]=>
array(2) {
["flightSegment"]=>
array(7) {
["departure"]=>
array(3) {
["iataCode"]=>
string(3) "BCN"
["terminal"]=>
string(1) "1"
["at"]=>
string(25) "2019-07-18T12:05:00+02:00"
}
["arrival"]=>
array(2) {
["iataCode"]=>
string(3) "AMS"
["at"]=>
string(25) "2019-07-18T14:30:00+02:00"
}
["carrierCode"]=>
string(2) "VY"
["number"]=>
string(4) "8302"
["aircraft"]=>
array(1) {
["code"]=>
string(3) "321"
}
["operating"]=>
array(2) {
["carrierCode"]=>
string(2) "VY"
["number"]=>
string(4) "8302"
}
["duration"]=>
string(8) "0DT2H25M"
}
["pricingDetailPerAdult"]=>
array(4) {
["travelClass"]=>
string(7) "ECONOMY"
["fareClass"]=>
string(1) "P"
["availability"]=>
int(8)
["fareBasis"]=>
string(5) "PRTVY"
}
}
}
}
}
["price"]=>
array(2) {
["total"]=>
string(6) "209.09"
["totalTaxes"]=>
string(5) "74.09"
}
["pricePerAdult"]=>
array(2) {
["total"]=>
string(6) "209.09"
["totalTaxes"]=>
string(5) "74.09"
}
}
}
}
}
["dictionaries"]=>
array(4) {
["carriers"]=>
array(2) {
["VY"]=>
string(16) "VUELING AIRLINES"
["IB"]=>
string(6) "IBERIA"
}
["currencies"]=>
array(1) {
["EUR"]=>
string(4) "EURO"
}
["aircraft"]=>
array(2) {
[320]=>
string(29) "AIRBUS INDUSTRIE A320-100/200"
[321]=>
string(21) "AIRBUS INDUSTRIE A321"
}
["locations"]=>
array(2) {
["AMS"]=>
array(2) {
["subType"]=>
string(7) "AIRPORT"
["detailedName"]=>
string(16) "SCHIPHOL AIRPORT"
}
["BCN"]=>
array(2) {
["subType"]=>
string(7) "AIRPORT"
["detailedName"]=>
string(7) "AIRPORT"
}
}
}
["meta"]=>
array(3) {
["links"]=>
array(1) {
["self"]=>
string(169) "https://test.api.amadeus.com/v1/shopping/flight-offers?origin=AMS&destination=BCN&departureDate=2019-07-16&returnDate=2019-07-18&adults=1&nonStop=true¤cy=EUR&max=2"
}
["currency"]=>
string(3) "EUR"
["defaults"]=>
array(1) {
["adults"]=>
int(1)
}
}
}
}
Now i have this:
foreach ( $flight_offers as $flights ) {
if (is_array( $flights ) ){
foreach ( $flights as $value ) {
echo $value;
}
} else {
echo $flights;
}
}

json_encode() doesn't return results?

So I have an array named $subcats like this :
Array
(
[83] => Array
(
[0] => Array
(
[id_sous_categ] => 4
[val] => filtre a essance
)
[1] => Array
(
[id_sous_categ] => 6
[val] => filtre AIR
)
)
[89] => Array
(
[0] => Array
(
[id_sous_categ] => 8
[val] => plaquette de frein
)
[1] => Array
(
[id_sous_categ] => 9
[val] => disque de frien
)
)
)
When i'm doing : $jsonSubCats = json_encode($subcats);
The result is empty.. What am I doing wrong ?
Edit : var_dump($subcats) shows :
array(7) { [83]=> array(4) { [0]=> array(2) { ["id_sous_categ"]=> string(1) "4" ["val"]=> string(16) "filtre a essance" } [1]=> array(2) { ["id_sous_categ"]=> string(1) "6" ["val"]=> string(10) "filtre AIR" } [2]=> array(2) { ["id_sous_categ"]=> string(1) "7" ["val"]=> string(14) "filtre a huile" } [3]=> array(2) { ["id_sous_categ"]=> string(2) "14" ["val"]=> string(16) "filtre a gasoile" } } [89]=> array(2) { [0]=> array(2) { ["id_sous_categ"]=> string(1) "8" ["val"]=> string(19) "plaquette de frein " } [1]=> array(2) { ["id_sous_categ"]=> string(1) "9" ["val"]=> string(15) "disque de frien" } } [84]=> array(6) { [0]=> array(2) { ["id_sous_categ"]=> string(2) "10" ["val"]=> string(23) "huile SAE 50 5L 5000 km" } [1]=> array(2) { ["id_sous_categ"]=> string(2) "11" ["val"]=> string(23) "huile SAE 50 1L 5000 km" } [2]=> array(2) { ["id_sous_categ"]=> string(2) "12" ["val"]=> string(22) "huile 15W40 5L 7000 km" } [3]=> array(2) { ["id_sous_categ"]=> string(2) "13" ["val"]=> string(22) "huile 15w40 1L 7000 km" } [4]=> array(2) { ["id_sous_categ"]=> string(2) "15" ["val"]=> string(22) "huile 10W40 5L10000 km" } [5]=> array(2) { ["id_sous_categ"]=> string(2) "16" ["val"]=> string(22) "huile 10W40 1L10000 km" } } [91]=> array(3) { [0]=> array(2) { ["id_sous_categ"]=> string(2) "17" ["val"]=> string(13) "joint culasse" } [1]=> array(2) { ["id_sous_categ"]=> string(2) "18" ["val"]=> string(20) "joint cache soupape " } [2]=> array(2) { ["id_sous_categ"]=> string(2) "19" ["val"]=> string(13) "joint carter " } } [86]=> array(6) { [0]=> array(2) { ["id_sous_categ"]=> string(2) "20" ["val"]=> string(6) "avant " } [1]=> array(2) { ["id_sous_categ"]=> string(2) "21" ["val"]=> string(7) "arrier " } [2]=> array(2) { ["id_sous_categ"]=> string(2) "22" ["val"]=> string(18) "amortisseur avant " } [3]=> array(2) { ["id_sous_categ"]=> string(2) "23" ["val"]=> string(18) "amortisseur arrier" } [4]=> array(2) { ["id_sous_categ"]=> string(2) "24" ["val"]=> string(28) "biellette suspension arriere" } [5]=> array(2) { ["id_sous_categ"]=> string(2) "25" ["val"]=> string(26) "biellette suspension avant" } } [96]=> array(1) { [0]=> array(2) { ["id_sous_categ"]=> string(2) "26" ["val"]=> string(6) "moteur" } } [80]=> array(1) { [0]=> array(2) { ["id_sous_categ"]=> string(2) "27" ["val"]=> string(15) "but�e embrayage" } } }
Thanks in advance.
json_encode fails to encode non-utf8 strings. Check your strings encoding. If that's the case, you have to either convert strings to utf8 or use your own implementation of json_encode (which you can quickly google by phrase "php json_encode implementation").
Make sure you have json support enabled in your server.
Run a simple <?php phpinfo() ?> in any php file and search for JSON.

Parsing particular value in JSON via PHP from Mapquest API

I am having trouble targeting the particular value in the array. I am trying to target the 'lng' and 'lat'. I feel like I'm close, and have tried several different ways to target. Currently trying this:
$json = file_get_contents($jsonurl);
$output = json_decode($json, true);
$latitude=$output['results']['locations']['latLng'][0];
$longitude=$output['results']['locations']['latLng'][1];
The JSON is:
array(3) {
["results"]=>
array(1) {
[0]=>
array(2) {
["locations"]=>
array(1) {
[0]=>
array(19) {
["latLng"]=>
array(2) {
["lng"]=>
float(-122.5008)
["lat"]=>
float(47.2629)
}
["adminArea4"]=>
string(13) "Pierce County"
["adminArea5Type"]=>
string(4) "City"
["adminArea4Type"]=>
string(6) "County"
["adminArea5"]=>
string(6) "Tacoma"
["street"]=>
string(0) ""
["adminArea1"]=>
string(2) "US"
["adminArea3"]=>
string(2) "WA"
["type"]=>
string(1) "s"
["displayLatLng"]=>
array(2) {
["lng"]=>
float(-122.5008)
["lat"]=>
float(47.2629)
}
["linkId"]=>
int(0)
["postalCode"]=>
string(5) "98406"
["sideOfStreet"]=>
string(1) "N"
["dragPoint"]=>
bool(false)
["adminArea1Type"]=>
string(7) "Country"
["geocodeQuality"]=>
string(3) "ZIP"
["geocodeQualityCode"]=>
string(5) "Z1XAA"
["mapUrl"]=>
string(186) "http://www.mapquestapi.com/staticmap/v4/getmap?key=Fmjtd|luub2gu2ll,7w=o5-9uaauz&type=map&size=225,160&pois=purple-1,47.2629,-122.5008,0,0|&center=47.2629,-122.5008&zoom=12&rand=-8386350"
["adminArea3Type"]=>
string(5) "State"
}
}
["providedLocation"]=>
array(1) {
["location"]=>
string(5) "98406"
}
}
}
["options"]=>
array(3) {
["ignoreLatLngInput"]=>
bool(false)
["maxResults"]=>
int(-1)
["thumbMaps"]=>
bool(true)
}
["info"]=>
array(3) {
["copyright"]=>
array(3) {
["text"]=>
string(22) "© 2013 MapQuest, Inc."
["imageUrl"]=>
string(35) "http://api.mqcdn.com/res/mqlogo.gif"
["imageAltText"]=>
string(22) "© 2013 MapQuest, Inc."
}
["statuscode"]=>
int(0)
["messages"]=>
array(0) {
}
}
}
Try
$latitude=$output['results'][0]['locations'][0]['latLng']['lat'];
$longitude=$output['results'][0]['locations'][0]['latLng']['lng'];

Categories