Php foreach remove duplicate using value [duplicate] - php

This question already has answers here:
How to remove duplicate values from a multi-dimensional array in PHP
(18 answers)
Closed 8 years ago.
I have an array like this
Array
(
[0] => Array
(
[idvox_business] => 5
[logo] => 1fJCb1_walmart_logo.jpg
[name] => Walmart
[icon] => http://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png
[lat] => 32.931393
[lng] => -96.840333
[miles] => 1
[place_id] => ChIJATuR8y0nTIYRQuI_GuSYqNs
)
[1] => Array
(
[idvox_business] => 41
[logo] => tqkuHo_bestbuy.png
[name] => Best Buy
[icon] => http://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png
[lat] => 32.9274
[lng] => -96.837284
[miles] => 1
[place_id] => ChIJVVVVVSknTIYReq3C1jDTAeM
)
[2] => Array
(
[idvox_business] => 41
[logo] => tqkuHo_bestbuy.png
[name] => Best Buy
[icon] => http://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png
[lat] => 32.927357
[lng] => -96.837275
[miles] => 1
[place_id] => ChIJs5gXoC0nTIYR-TFzaCpxVvE
)
[3] => Array
(
[idvox_business] => 16
[logo] => C25fIa_macys_logo.jpg
[name] => Macy's
[icon] => http://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png
[lat] => 32.92978
[lng] => -96.81953
[miles] => 2
[place_id] => ChIJWb8Ag9sgTIYRiPwoJIqw2rc
)
[4] => Array
(
[idvox_business] => 41
[logo] => tqkuHo_bestbuy.png
[name] => Best Buy
[icon] => http://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png
[lat] => 32.956207
[lng] => -96.815811
[miles] => 3
[place_id] => ChIJnVzmzT0hTIYRsWuHSl2p1Ig
)
)
I want to remove duplicates from this array but i need to omit only last unique array output need like this
Array
(
[0] => Array
(
[idvox_business] => 5
[logo] => 1fJCb1_walmart_logo.jpg
[name] => Walmart
[icon] => http://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png
[lat] => 32.931393
[lng] => -96.840333
[miles] => 1
[place_id] => ChIJATuR8y0nTIYRQuI_GuSYqNs
)
[1] => Array
(
[idvox_business] => 41
[logo] => tqkuHo_bestbuy.png
[name] => Best Buy
[icon] => http://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png
[lat] => 32.9274
[lng] => -96.837284
[miles] => 1
[place_id] => ChIJVVVVVSknTIYReq3C1jDTAeM
)
[3] => Array
(
[idvox_business] => 16
[logo] => C25fIa_macys_logo.jpg
[name] => Macy's
[icon] => http://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png
[lat] => 32.92978
[lng] => -96.81953
[miles] => 2
[place_id] => ChIJWb8Ag9sgTIYRiPwoJIqw2rc
)
)

Since the sub-arrays are not identical then the serialize solution will not work. This assumes removing based on identical idvox_business:
$ids = array();
foreach($array as $key => $vals) {
if(in_array($vals['idvox_business'], $ids)) {
unset($array[$key]);
} else {
$ids[] = $vals['idvox_business'];
}
}
Or possibly:
foreach(array_column($array, 'idvox_business') as $key => $id) {
$result[$key] = $array[$key];
}

Related

Show multiple api result photos on google map

Get 20 results of (example:School ) from google places api and i want to show photos on google map but some api results does not contain any image so how i can fix it. Error show: undefined index 'photos'.
if ($resp['status']=='OK') {
//Loop through get the multiple results
for ($i=0; $i < count($resp['results']) ; $i++) {
//Get the Important Data
$data['formatted_address'] = $resp['results'][$i]['formatted_address'];
$data['latitude'] = $resp['results'][$i]['geometry']['location']['lat'];
$data['longitude'] = $resp['results'][$i]['geometry']['location']
enter code here['lng'];
$data['icon'] = $resp['results'][$i]['icon'];
$data['name'] = $resp['results'][$i]['name'];
$data['height'] = $resp['results'][$i]['photos'][0]['height'];
$data['html_attributions'] = $resp['results'][$i]['photos'][0]
['html_attributions'][0];
$data['photo_reference'] = $resp['results'][$i]['photos'][0]
['photo_reference'];
$data['width'] = $resp['results'][$i]['photos'][0]['width'];
//Verify if Data Exist
if (!empty($data) && !empty ($data['latitude']) && !empty
($data['longitude'])) {
print_r($data);
}else{
echo "<h1 style=color:red;>Values Empty</h1>";
}
}//End for-loop
}else{
echo "<h1 style=color:red;>Status Error</h1>";
}
Get the result of schools from google places api the index[1] does not contain any image than how to show on map.
[results] => Array(
[0] => Array
(
[formatted_address] => Tariqabad Multan, Punjab, Pakistan
[geometry] => Array
(
[location] => Array
(
[lat] => 30.2073928
[lng] => 71.4530978
)
[viewport] => Array
(
[northeast] => Array
(
[lat] => 30.208742629893
[lng] => 71.454447629893
)
[southwest] => Array
(
[lat] => 30.206042970107
[lng] => 71.451747970107
)
)
)
[icon] =>
https://maps.gstatic.com/mapfiles/place_api/icons/school-71.png
[id] => de1405f04fffe5f2910789717344e513625aada3
[name] => Learner's Castle School Multan
[photos] => Array
(
[0] => Array
(
[height] => 730
[html_attributions] => Array
(
[0] => Faisal Mushtaq
)
[photo_reference] =>
CmRaAAAAvhDiC7bMqpf3F5lvLSUmDkv8aAqSF1AaC_EoeUhG
f40GCU3H0zaLOQZ96fRZxTJ9goxxh2C
CfJXtJyXfMOBBqPPsOAKenERrefpNwtdV07nDyPE62nSdz3aO7T1aQaMy
EhBQOTSeTEKvNxBCk5rxl7 FUGhQ9MSlemG_o0jxeYE9f0_RmtnQzrQ
[width] => 1296
)
)
[place_id] => ChIJqfKsD8IzOzkRomMlLG80aho
[plus_code] => Array
(
[compound_code] => 6F43+X6 Multan, Pakistan
[global_code] => 8J2H6F43+X6
)
[rating] => 5
[reference] => ChIJqfKsD8IzOzkRomMlLG80aho
[types] => Array
(
[0] => school
[1] => point_of_interest
[2] => establishment
)
)
[1] => Array
(
[formatted_address] => N Gulgasht Blvd, Basti Mehmood Kot,
Multan, Punjab 60000, Pakistan
[geometry] => Array
(
[location] => Array
(
[lat] => 30.2464553
[lng] => 71.483087
)
[viewport] => Array
(
[northeast] => Array
(
[lat] => 30.247805129893
[lng] => 71.484436829893
)
[southwest] => Array
(
[lat] => 30.245105470107
[lng] => 71.481737170107
)
)
)
[icon] =>
https://maps.gstatic.com/mapfiles/place_api/icons/school-71.png
[id] => 8648cd48c320f11b001a0eece814bd72abc07231
[name] => Zakariya Public School
[opening_hours] => Array
(
[open_now] => 1
)
[place_id] => ChIJ5c1N-YM0OzkRKKtxXEhilow
[plus_code] => Array
(
[compound_code] => 6FWM+H6 Multan, Pakistan
[global_code] => 8J2H6FWM+H6
)
[rating] => 4.4
[reference] => ChIJ5c1N-YM0OzkRKKtxXEhilow
[types] => Array
(
[0] => school
[1] => point_of_interest
[2] => establishment
)
)
[2] => Array
(
[formatted_address] => 17- Officers Colony, Officers Colony,
Multan, Punjab, Pakistan
[geometry] => Array
(
[location] => Array
(
[lat] => 30.2105142
[lng] => 71.4726681
)
[viewport] => Array
(
[northeast] => Array
(
[lat] => 30.211864029893
[lng] => 71.474017929893
)
[southwest] => Array
(
[lat] => 30.209164370107
[lng] => 71.471318270107
)
)
)
[icon] =>
https://maps.gstatic.com/mapfiles/place_api/icons/school-71.png
[id] => 8c809b4c5a4b8355899ddf6b6a7497bb8cb43be1
[name] => Jinnah Highs School System
[opening_hours] => Array
(
[open_now] => 1
)
[photos] => Array
(
[0] => Array
(
[height] => 1920
[html_attributions] => Array
(
[0] => A Google User
)
[photo_reference] =>
CmRZAAAAmQ8VIRfu9_s5UTrmBRwHpPWYuPjXWWcRj5KnWGwTy4yXo-
h2bWa8NSXrYSI8ukYD8nhfQRVxjxVKfWMxJlC4Xn8D9KBmoB7M9P
CZU5_XHvZ2heWSXPkqcGvjlcDiMalsEhA3VzcWjoDnL9Fs7Bs8PP
t4GhSePGS5CmCg0AXA8kEfl91iWotZgA
[width] => 2560
)
)
[place_id] => ChIJz4eKo_YzOzkRHvCbt43xkq8
[plus_code] => Array
(
[compound_code] => 6F6F+63 Multan, Pakistan
[global_code] => 8J2H6F6F+63
)
[rating] => 4.3
[reference] => ChIJz4eKo_YzOzkRHvCbt43xkq8
[types] => Array
(
[0] => school
[1] => point_of_interest
[2] => establishment
)
)
you should first check if photo key is exist in $resp['results'][$i] array by using isset function in php
for example
if(isset($resp['results'][$i]['photos']) && count($resp['results'][$i]['photos']) > 0)
{
// write your code
}
and also you should use phpenter code here count function to check $resp['results'][$i]['photos'] array has multi entry
so you can write $resp['results'][$i]['photos'][0] without have out of scope error

getting array value from json array PHP

i am using google place json array
json array
i am using this code to get name of place
$string = file_get_contents("https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&rankby=distance&types=food&key=AIzaSyCpOG91fkAzPTZiFq7G9HbLfbmVyW8Wgyc");
$json_a = json_decode($string, true);
echo print_r(array_values($json_a-> results[0]->name[0]));
but the result always { 1 } where the result should be Biaggio Cafe
here if i print out the array print_r($json_a)
Array ( [html_attributions] => Array ( ) [next_page_token] => CqQCFwEAAN1f0rbnMkVsdezGGV9kDlKiYdkDKgOSeaul7JL6GFEeLhV0-h3_WHa0NGbQ_here u_N7K3e7r5xxtW4FGjp6dHK4WKxUb1LPbmLq4KvybiR7QyYspK6wHC4In7Slnhj_0veXd25lruweEKFTmnu6fX0JUN3Lwtyz4KnQrnGeJHX7Ldr6oWJcSn-ilM2AXCQi0LnFza4dJmlvh62Eq583kQ5s1WQO8VrnQ8G66Yfy69X7Glbe0Pijj384gSWncN6kEbEUAw_PyHe4R3z50MRa336ZpzTxOulRwjV6NtNtUbn-j-omj0fb2Q0Yf6QQRrmiAaKx2QqYaN3AB7oH1YouLKLyGVzA3qOoIdYmI3OAztgkPKWUlNl5PzS3Ro7V8g-RdNN0hIQTtLlDEzmNPviMy1X28gkHBoUQxlMdi3EX_zrQzljRF12sjVab2c [results] => Array ( [0] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.8669667 [lng] => 151.1958862 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.865664669709 [lng] => 151.19708633029 ) [southwest] => Array ( [lat] => -33.868362630292 [lng] => 151.19438836971 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/cafe-71.png [id] => 7eaf747a3f6dc078868cd65efc8d3bc62fff77d7 [name] => Biaggio Cafe [opening_hours] => Array ( [open_now] => [weekday_text] => Array ( ) ) [photos] => Array ( [0] => Array ( [height] => 3036 [html_attributions] => Array ( [0] => Briggs Soria ) [photo_reference] => CmRaAAAAcPXEEuCbXQiIU-c0rFzFwHhpmRPEDkZ-7D6ep2gpkEG8Yyx6-aB6o4cp2LWce728a-Eb9izN9ju-TAe6sdURdcpX-UpJwMHR7Ynj1H4HYy7R4WUWtqxbN7hifOWqV6TzEhD0g0k5X-KCKBu9RdWpl-47GhQwPBWXcBL0LnMWEp9ItV7REPcmTQ [width] => 4048 ) ) [place_id] => ChIJIfBAsjeuEmsRdgu9Pl1Ps48 [price_level] => 1 [rating] => 3 [reference] => CmRSAAAAiI3HIH3xQoIx0spB-d1aye7c7W4L3s25RxdAHwwLVEvC5AovkxwscFsfi-1ZKAL9V-DAPda5d9FH_ywnl7Ll3vovPw5v6T08uNxVwJyMn9MPJFHWZN06dJT1f50vZ19qEhBfOVCaYhc8f9tk2UZLkgAOGhSSKLENUQ1taZOQWolQVsJ0V48bQA [scope] => GOOGLE [types] => Array ( [0] => cafe [1] => restaurant [2] => food [3] => store [4] => point_of_interest [5] => establishment ) [vicinity] => 48 Pirrama Road, Pyrmont ) [1] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.8671288 [lng] => 151.195339 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.866065569709 [lng] => 151.1960316 ) [southwest] => Array ( [lat] => -33.868763530292 [lng] => 151.1932612 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png [id] => 8bdf85385635caa3b9df55ec23d533ceb0f2b8c6 [name] => The Century [opening_hours] => Array ( [open_now] => 1 [weekday_text] => Array ( ) ) [photos] => Array ( [0] => Array ( [height] => 1989 [html_attributions] => Array ( [0] => The Century ) [photo_reference] => CmRaAAAAbC2XEW4KdedbESdFb1Bgija2OV_9mR8t6DJqMZwmLjwXAtG5JgWYfzUi7nEx0ZKqhhsDxtPceC3MPKbYJIVh7VWT_CDvsN7hRtW2bdpxss87I9DbTn450Vd7dn0wOueWEhBGiONsANF-eOu1xwrVMXP4GhTqtBdrIP84yK19IXGwZVqazpEACQ [width] => 2048 ) ) [place_id] => ChIJ1-v38TauEmsRNrXszdcSywQ [price_level] => 2 [rating] => 3.5 [reference] => CmRRAAAAyJYatPQTSvqMbOt1gIhNxfc1Sy7fnXKDBaX0lYkx16_u2HEu8hyBH_XriKc1Aciq3Gx8FxQI_OkXxRTFh2xz8eJbV8lOA6gFoMxDbvV3nQN2yCAwwgwo60wTKavRnxcOEhBT6whUoikUKFrq4EXRcK4OGhRgUy8KHjA9IS8ckfED_CNkdScvvg [scope] => GOOGLE [types] => Array ( [0] => restaurant [1] => food [2] => point_of_interest [3] => establishment ) [vicinity] => The Star, 80 Pyrmont Street, Pyrmont ) [2] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.8667062 [lng] => 151.1957844 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.865427419709 [lng] => 151.19697313029 ) [southwest] => Array ( [lat] => -33.868125380292 [lng] => 151.19427516971 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/cafe-71.png [id] => fd0f490655de01f49b604781f944340f6623d1b1 [name] => Biaggio Cafe [opening_hours] => Array ( [open_now] => [weekday_text] => Array ( ) ) [place_id] => ChIJA0NrtTeuEmsRkmB_OMsgGOw [reference] => CmRSAAAANgrbbAHDKaIJ1OR0BFJ6t2Jlk0fKKDN9-hnfaDtSU2Xirm_66SOV4Wpy_w7HL6WVv0JXy4vrUdWkLMVizwGS1XpjSqDs-M6WbzSyGr6C-gJdLCUwDMPf6RBpCfeo279REhApljm2GRCvCVzFqcZ0pkGqGhRttJFZd6bXU67b3i-YN-N7ucP2oA [scope] => GOOGLE [types] => Array ( [0] => cafe [1] => restaurant [2] => food [3] => point_of_interest [4] => establishment ) [vicinity] => 48 Pirrama Road, Pyrmont ) [3] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.8667062 [lng] => 151.1957844 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.865357219708 [lng] => 151.19713338029 ) [southwest] => Array ( [lat] => -33.868055180291 [lng] => 151.19443541971 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png [id] => 3ef986cd56bb3408bc1cf394f3dad9657c1d30f6 [name] => Doltone House - Darling Island Wharf [opening_hours] => Array ( [open_now] => [weekday_text] => Array ( ) ) [photos] => Array ( [0] => Array ( [height] => 843 [html_attributions] => Array ( [0] => Doltone House - Darling Island Wharf ) [photo_reference] => CmRaAAAAkdwvY7mKPHP6aCqmXjNPithGEIBaytETIG8GtU70vtAsymKWMaYhw4-VW6_W3HKyH9ddeVjnRFWwmWFlZO2yNLZ8IPDGmPSbqqtA4TzwZL9XddUH1eBGg-BhlicwYUTYEhAnqcUR2JQcwa8O54DUD1_XGhRGg60_1NqarQ1UbtXiJzaPq5MtxA [width] => 1500 ) ) [place_id] => ChIJ5xQ7szeuEmsRs6Kj7YFZE9k [rating] => 4.2 [reference] => CmRSAAAAVzanf0yiJg-X4Y2F94Wj-SjxZLi7i4zwrXM-0mEkXgmzelK7Jus6fGWbKsqeGH8fSbzn8oNBGhyV5VvewU4AXFoUx5uMjrg6A5Ho8vccsfL0Dq5PMReQiXYUKYBDoXPNEhCmcU6_Vat2qDmjKaPtJIeYGhT2c8QasUnZCrUQpd_RFUO1_Jf6lQ [scope] => GOOGLE [types] => Array ( [0] => food [1] => point_of_interest [2] => establishment ) [vicinity] => Ground Floor, 48 Pirrama Rd, Pyrmont ) [4] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.8667062 [lng] => 151.1957844 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.865357219708 [lng] => 151.19713338029 ) [southwest] => Array ( [lat] => -33.868055180291 [lng] => 151.19443541971 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png [id] => 2de2d85c0465dafc9a75e0304859c5ceedb4a679 [name] => Signorelli Gastronomia [opening_hours] => Array ( [open_now] => [weekday_text] => Array ( ) ) [photos] => Array ( [0] => Array ( [height] => 390 [html_attributions] => Array ( [0] => Signorelli Gastronomia ) [photo_reference] => CmRaAAAA5VEkXQg5NXRvvbjQ-sHCF1rusIPBRItqIXb7ctqawTC8_ZG4b0zPBGBLSA8ttYHp1D2DFFaqT0WtsXLT0tmSBXnJ47-Ei94jKjg5jCIpWt5dlweWMMyx5duOEBNB2biLEhBZqLCcaoSa2tVU6zZEWewGGhQ9JSj_mVqMDeizYtrcYVaCYVz6sQ [width] => 960 ) ) [place_id] => ChIJFcJDMzauEmsRy81NJCujoY8 [price_level] => 2 [rating] => 3.9 [reference] => CmRSAAAAJcg6WX2L8zTXoizEVUB5lyaKW22-sMoMsiOBaT8Oa4stB2PrbrObqZ2boBIIR8xxpyFIpf7q-HZuuDp3oXJ4Wa964l1koOq7jEjk7l_2wFHWdAsEF0ekpmvZV4rTC_nTEhAfwAlxYeqXvgmAL5vBlEjuGhSkxSkyhxCJOZo2gr4lUa7YU5NSww [scope] => GOOGLE [types] => Array ( [0] => food [1] => point_of_interest [2] => establishment ) [vicinity] => Ground Floor, 48 Pirrama Road, Pyrmont ) [5] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.8664812 [lng] => 151.196061 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.865132219708 [lng] => 151.19740998029 ) [southwest] => Array ( [lat] => -33.867830180291 [lng] => 151.19471201971 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png [id] => 91509349e192972859b886e62f11827e5bd0072f [name] => Esky Cafe [opening_hours] => Array ( [open_now] => [weekday_text] => Array ( ) ) [photos] => Array ( [0] => Array ( [height] => 1536 [html_attributions] => Array ( [0] => Paul Toogood ) [photo_reference] => CmRaAAAAWHv_VoJ9mDttpdl8XXeM78_bDtutVfhNIwoz57Va1CEzOrxzN5XAtuu1OwzwU8NoH6prpkLzdALY0DhdKz4reJkejB_AV4NYrbFbztRRG5xcJGthqTHOzzdDXzBTkPk-EhDVmJVlHn-tap_okguRQ4eKGhQ78ZojGOFEKHxpPeJGTBaNa68yDg [width] => 2048 ) ) [place_id] => ChIJm3d7yTeuEmsRC2wYUB24q9I [rating] => 4.4 [reference] => CmRSAAAAOEtb2LjP1kZkWxsgcq5mGm6NWlAlwMOTXQnF3HFvwRN9-cIl0-RJU8-nT3Jp8dRlib9o0M2U0JFauLwjjOw0fqPadh2OBVqGde8LqbzVP4FUIlmKR-lKOCuC1U8Fyw1QEhB0T-m_Jh_h4ooBbl4YxQQOGhQC_bkk9d2Fyraihp5Q6-njdS1QEg [scope] => GOOGLE [types] => Array ( [0] => restaurant [1] => cafe [2] => food [3] => point_of_interest [4] => establishment ) [vicinity] => Workplace 6, 48 Pirrama Road, Pyrmont ) [6] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.8677499 [lng] => 151.1956285 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.866673069708 [lng] => 151.19631778029 ) [southwest] => Array ( [lat] => -33.869371030291 [lng] => 151.19361981971 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png [id] => b21cfc23b85bda32717dc32bbb3b0d290e1692bf [name] => Kampung [opening_hours] => Array ( [open_now] => [weekday_text] => Array ( ) ) [place_id] => ChIJ1-v38TauEmsRkfBaAXNA5LI [rating] => 5 [reference] => CmRSAAAAh9Vuux8EylTbKIFyoWDxeWXxXHp8fsNM7Q97h8W9_TxvAo23lTjahLW0vGFAnlmHG3qIu4WXfo5XlgS42tQIsp5RiAiFiuqLtlXX1uRI4JxJrpi3oSxCp75fRlxGJsgrEhB2JLQK_7pdw-RhP6XDi12XGhTcmREuPiKty8znAmNvc1VPE2Unvg [scope] => GOOGLE [types] => Array ( [0] => restaurant [1] => meal_takeaway [2] => food [3] => point_of_interest [4] => establishment ) [vicinity] => 80 Pyrmont Street, Pyrmont ) [7] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.8677499 [lng] => 151.1956285 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.866673069708 [lng] => 151.19631778029 ) [southwest] => Array ( [lat] => -33.869371030291 [lng] => 151.19361981971 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png [id] => f67efa5095d123ed2c1ce28e9297cc1e04624dc8 [name] => Lotus Pond [opening_hours] => Array ( [open_now] => 1 [weekday_text] => Array ( ) ) [place_id] => ChIJS_-h7TauEmsRdmDJ2CvXToY [rating] => 3.2 [reference] => CmRSAAAApnMGhFr1SKg2H4LpIDfJXqOAy2OUDSy_NaWA6ZOetI47fBG-Un_heBunbN2ozjyuhac5z7MHXOCwlGchZRpOJtOJupqfhOHGP93S2fvSTUt8nLUUo39WKBvpHUMrfCKmEhBuSr4HMwgKwUJkpjT5ffUnGhRQejzefZ3S3vCStqCxF8wAlBt1JQ [scope] => GOOGLE [types] => Array ( [0] => restaurant [1] => food [2] => point_of_interest [3] => establishment ) [vicinity] => 80 Pyrmont Street, Pyrmont ) [8] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.8677499 [lng] => 151.1956285 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.866673069708 [lng] => 151.19631778029 ) [southwest] => Array ( [lat] => -33.869371030291 [lng] => 151.19361981971 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/bar-71.png [id] => 882abf952e4d0e6dd96294459abe076c90c127dd [name] => 24/7 Sports Bar [opening_hours] => Array ( [open_now] => 1 [weekday_text] => Array ( ) ) [photos] => Array ( [0] => Array ( [height] => 638 [html_attributions] => Array ( [0] => 24/7 Sports Bar ) [photo_reference] => CmRaAAAAYsUMTqwU7RRG7gB1ft1yChuVhIvF4_TZ1NBRyyQdQs163bRXTh0X2ZJ_4AzjnNQztgZgOFxieDONaMGfqBlfQ3GWjgMFPZ3m-Ut-UsumAyDi2FQoo25vDBp58CA2kfdwEhDhNViiTe0vPZz2FgiFPphqGhTx1ZfQLwYRebnXV4I0q8yS1VF1Sg [width] => 960 ) ) [place_id] => ChIJ77Cd7TauEmsRBV42CMtSans [rating] => 3.4 [reference] => CmRRAAAANyqljC8u7sy67zaoKmvy1yZF9BJcoCO4UBwiC0F6WIPMkhZo84UbBiyR_bngCy8mziKacKR7hqOmH0C5YqJryYPNSpHqeTahBB84i7wnezFrFYklzHCjDTt_DhDFW96mEhBXRQVuWptLLxd9XRES6X3pGhQF3k1348GfUaIlUVmFiE06ERWP6w [scope] => GOOGLE [types] => Array ( [0] => bar [1] => restaurant [2] => food [3] => point_of_interest [4] => establishment ) [vicinity] => 80 Pyrmont Street, Pyrmont ) [9] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.8677499 [lng] => 151.1956285 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.866673069708 [lng] => 151.19631778029 ) [southwest] => Array ( [lat] => -33.869371030291 [lng] => 151.19361981971 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png [id] => e188acf29a29bc46f1cf844c0bf78f8b1464bd6f [name] => BLACK Bar & Grill [opening_hours] => Array ( [open_now] => [weekday_text] => Array ( ) ) [photos] => Array ( [0] => Array ( [height] => 2160 [html_attributions] => Array ( [0] => Chopinand Mysaucepan ) [photo_reference] => CmRaAAAAwz1MNHctvLtTgc7iFYmuDljnVuWyKnajNBh0z24Il7cTm7TAiJlAlnzvsgUp5JVULO8_oPFxW6DkjKLqN-MI0T703nJzFYGT79Yx36tJxuZaBAkZ5xIAis7T51pXYQ-wEhCupMDWqDDxv_3YsfJZnsQPGhQBp2vGHZWl6ECDGdoGt17Hz28PJg [width] => 3232 ) ) [place_id] => ChIJ9ZCzFzGuEmsR_EwB_qra-W4 [rating] => 4.1 [reference] => CmRRAAAAkRe25ds64LLF7mzx0Qdiy-d2kqa1WQzeNHH6_SsiDeZFdzjorMWenC7z4VRTw7BdNocoPzjqb6AjICSKQZCR6fW_xVHmsEUQNJseFAJ-6hDlgv2PpoBJ9pU5PnK221T3EhBJ3CwsMrtaSdukyZyDIpWvGhSeLY4vgz6nO6ZSW5bb0gcQsJxIOQ [scope] => GOOGLE [types] => Array ( [0] => restaurant [1] => bar [2] => food [3] => point_of_interest [4] => establishment ) [vicinity] => G, 80 Pyrmont Street, Pyrmont ) [10] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.8677499 [lng] => 151.1956285 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.866673069708 [lng] => 151.19631778029 ) [southwest] => Array ( [lat] => -33.869371030291 [lng] => 151.19361981971 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png [id] => 8ed2a95d07a89918c0a1b846544a955b36820af3 [name] => Dergah Grill [opening_hours] => Array ( [open_now] => 1 [weekday_text] => Array ( ) ) [photos] => Array ( [0] => Array ( [height] => 992 [html_attributions] => Array ( [0] => Dergah Grill ) [photo_reference] => CmRaAAAAjsOwnxHbfcRXNSsW7sVFA88bbK7XoeeI8etp5i3Rkmfvw2Nv2oOGbVoJzNZrzUwBrFpTRv_pU27OzahrLlCUFFLGz-_P2Z7Hn4jH0waC4urQg18yPbyE2qHFXRAh2sF_EhBK6Dk08w1Z44NTsgzbBVSMGhRLCr0qizP00LjX_T6jUh_986vnWA [width] => 1487 ) ) [place_id] => ChIJ1-v38TauEmsR7cWnOpSlMWU [rating] => 3 [reference] => CmRRAAAAz-ES7KMUShGEIn28_6aURVGtGr3kEJR9SzWKTgvXo9QDYSJAIVSe2WD02y4H9aJEv3IXJUTl1wH4iMxivWmNwNNJoSCotqKo7Dky9K4dpVSKwSLJvyGA65G1tuHzswMzEhAz3rOaYTJh9qr42kVmFeFdGhQwYDAjO2IYp6ypv5ot-QFZvZzJOA [scope] => GOOGLE [types] => Array ( [0] => meal_takeaway [1] => restaurant [2] => food [3] => point_of_interest [4] => establishment ) [vicinity] => 80 Pyrmont Street, Pyrmont ) [11] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.8677499 [lng] => 151.1956285 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.866673069708 [lng] => 151.19631778029 ) [southwest] => Array ( [lat] => -33.869371030291 [lng] => 151.19361981971 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/cafe-71.png [id] => 5895b175f4c2a8535294f5587fe089fc7d5135ea [name] => Pulse Express [opening_hours] => Array ( [open_now] => [weekday_text] => Array ( ) ) [photos] => Array ( [0] => Array ( [height] => 992 [html_attributions] => Array ( [0] => Pulse Express ) [photo_reference] => CmRaAAAAg1N3QTCieHLyOJE0-UsbTQJJnawb4ZTuoB_-WPf9Wz4fOQbtJGvRWbUnppV_RgKaq5heQuEMBGg9kI0iXGxrDVPOEnQ3Zyh-iLbge3xpuZyQI0mjS6BlvyxRdNPvn3lVEhChKRAL5I5nHP_QU_a7aJjWGhTlwb7eywRH-kh7dx-spnPvP4bUXA [width] => 1618 ) ) [place_id] => ChIJ1-v38TauEmsRJxe1QBBHBGU [rating] => 2.6 [reference] => CmRRAAAALsYqD7jAxdAy8zpoDEWxfFD9PFNlJdN8o-krm3fupp77oxNnP-fJJqvplpFIxWTFA1pjVwFG0ozgeao2PhoL0VoKPDT6Gy6qi56NKyusNn6eDkfzLctO3iDB4Dx_inomEhAjhFof0CLWU1vFJ4VMOfvjGhSVYSHLz7kQml-Ne-VJVJuawxywRg [scope] => GOOGLE [types] => Array ( [0] => cafe [1] => restaurant [2] => food [3] => point_of_interest [4] => establishment ) [vicinity] => 80 Pyrmont Street, Pyrmont ) [12] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.8678337 [lng] => 151.1958116 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.866484719709 [lng] => 151.19716058029 ) [southwest] => Array ( [lat] => -33.869182680291 [lng] => 151.19446261971 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png [id] => f138865c7ac72dcc2cbd0491710aa6203ebc595f [name] => Pizzaperta [opening_hours] => Array ( [open_now] => 1 [weekday_text] => Array ( ) ) [photos] => Array ( [0] => Array ( [height] => 440 [html_attributions] => Array ( [0] => Pizzaperta ) [photo_reference] => CmRaAAAADCeKPVnBAGuxYOy03lNLG-7HuR5VQ6fBNFQmvJsCX52UBYB_wV4_450EskliO7fGWAwZ6mXhNLgonnqp0I9ixGFOeWzBuT2ChqPPoET9ofpKYiwNi9t4d7fl5NQTYyc5EhCxeM8QlMlbKP65Mku5ZAnDGhQR-zt6WCXpQXmhJ5gmXSdamamTWQ [width] => 700 ) ) [place_id] => ChIJA6-_qzeuEmsRiV9SAHIbzh4 [rating] => 3.4 [reference] => CmRRAAAAn-uUOFjUKSV18ENqqy65iOzr3Ea1H3ipK-9c3UX1IsTJPgb_-gd6d4BrsUIDcKZmwAnQZ8rtesI8jA0V46-9ovQh9RiGXmJsLrz3UyrKSxkhOuLXvFzyKu2z3Ja8QbGMEhDo-m7u5hyHfMmgPTcdKsWOGhRo5Fizn7eEefTbuOl9D8GfMAmX8A [scope] => GOOGLE [types] => Array ( [0] => restaurant [1] => food [2] => point_of_interest [3] => establishment ) [vicinity] => The Star Sydney, Harbourside Entry, Pirrama Rd, Pyrmont NSW 2009, Sydney ) [13] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.867931 [lng] => 151.1958456 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.866582019709 [lng] => 151.19719458029 ) [southwest] => Array ( [lat] => -33.869279980292 [lng] => 151.19449661971 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png [id] => 372b98902d4032b2708939131b8bc6bfbce9044c [name] => Chao Praya City [place_id] => ChIJ1-v38TauEmsRwCiun0VT1Kg [rating] => 5 [reference] => CmRSAAAA4HOsgFazyJOqUAEAfPyuQif3apB-IrYQ_UVZTyvwYLm18txQlrvMOWcaZp98ggKyWEoA0ea17TSlUNwHxJt3YLAOY_Aj6JSaOtRWxl3HdIoGwFGpVzVo37OyXen7d5nCEhDy43UD1zpBwgahbZGtW7nBGhRiLaMA4r9IsOXzLTe7-tm-ZzDNRA [scope] => GOOGLE [types] => Array ( [0] => restaurant [1] => food [2] => point_of_interest [3] => establishment ) [vicinity] => P1 Level G Star City Pirrama Road, Pyrmont ) [14] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.8680279 [lng] => 151.195857 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.866961369708 [lng] => 151.1965416 ) [southwest] => Array ( [lat] => -33.869659330291 [lng] => 151.1938032 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png [id] => 08c0f74375ca1956d8ae2919512171adcd8266d7 [name] => Balla [opening_hours] => Array ( [open_now] => [weekday_text] => Array ( ) ) [photos] => Array ( [0] => Array ( [height] => 2882 [html_attributions] => Array ( [0] => Cynthia Chia ) [photo_reference] => CmRaAAAAQ3bhhMFaAoi-XSdDMIl1abzVHbm92X575sZ--9E828fgDgt1va8s8to_iCVMjruKD-E2vECITkXW_hwBP9iS4NE8fJSyEj2ZrK6eCP5HAiafZJT0DUsRkiWxVQ66Ud8mEhBDEgwL9JaYlPOaTIlT81MpGhTK_Bmrs14OfusC2d7vJ3SoRIUiKw [width] => 3843 ) ) [place_id] => ChIJ1-v38TauEmsRFBHuvMmwNyA [price_level] => 3 [rating] => 3.7 [reference] => CmRRAAAAxa4gkSU26kt1A9BMsaxySAdOysJGpreX08qD8FY-w40v6J36GYo1xIZJ9vy-T8zbjNLXr1PEXD458O72KpZr83kNlEuZIMYxQcodSdkAFtMJzbsaCCm2ldzYjeF-cDM1EhDLMSnhPjgnj33vJn13HV6GGhRNG8bcAGL_d9kMXxMMxfJjjGyffA [scope] => GOOGLE [types] => Array ( [0] => restaurant [1] => food [2] => point_of_interest [3] => establishment ) [vicinity] => The Star, G, 80 Pyrmont Street, Pyrmont ) [15] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.8679022 [lng] => 151.1951276 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.866727419708 [lng] => 151.19605433029 ) [southwest] => Array ( [lat] => -33.869425380291 [lng] => 151.19335636971 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/cafe-71.png [id] => 0761a21e23eda2598330d428413cb59431316a61 [name] => Lobby Lounge [opening_hours] => Array ( [open_now] => 1 [weekday_text] => Array ( ) ) [photos] => Array ( [0] => Array ( [height] => 1632 [html_attributions] => Array ( [0] => Andrei Setiawan Yohannes ) [photo_reference] => CmRaAAAA5WtwRlnY-IB_agGsXdnruKoylr826EKf-vmptRBP68TXnQPuT5DPGByyl9VCMiC-_tt7PGY-qVVoXQnEue-fS9S2oWyyiUoXA_XTnriNyC0TasEwfyqMI2tHnfEn4r_5EhBZpIz5HXkqmqBK6HQhxQ4yGhRBLF-Xg9yILQPoB8KW4bT6xJn1Ag [width] => 920 ) ) [place_id] => ChIJ1-v38TauEmsRHbUt24abGq8 [rating] => 3.9 [reference] => CmRSAAAALvC6LDeHGtKa-1BfrK8GxGZJms5-W23jkxgNHUNfUAWST9IIfNzcqPKy7MMuaLztr5sIZpIiEs5YBhM4PtAccuBSdJSx5LaWK-E5ObIs_LZPCwtOikhG0NMtAf6D_hbpEhD2eT7U7sfSDnKg98_RWkd8GhQD3C5E8vTUxAO_p1GxO7w5oYtcCw [scope] => GOOGLE [types] => Array ( [0] => cafe [1] => food [2] => store [3] => point_of_interest [4] => establishment ) [vicinity] => 80 Pyrmont Street, Pyrmont ) [16] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.8679658 [lng] => 151.1952022 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.866350469709 [lng] => 151.19779985 ) [southwest] => Array ( [lat] => -33.869048430291 [lng] => 151.19280965 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/lodging-71.png [id] => 44fdc977984610cd873c790a7d850e8185ed0e5e [name] => The Star Sydney [opening_hours] => Array ( [open_now] => 1 [weekday_text] => Array ( ) ) [photos] => Array ( [0] => Array ( [height] => 1978 [html_attributions] => Array ( [0] => The Star Sydney ) [photo_reference] => CmRaAAAAAKL4p8pfqn9KTX1IP4mC6wbRpfJB2R8wdaAT7y6fmdLL8-5XIdgggJp31eE9S0tJQbifbUqDCCjqH1YK74u2ezYck-6_m7MB7FG9fBCKcW_gv5Utyt5nHcC2IiW7GpfFEhCNnJ_cBhkAaSf9vcmYiQ0SGhROkORa05CJzj2d8kBLLRdg6q39OA [width] => 2048 ) ) [place_id] => ChIJq6qq6jauEmsRJAf7FjrKnXI [rating] => 4 [reference] => CmRRAAAAP2aFIXavvX5VXtZkh_BsYrNfKlzYMJS-LeoZWPUzOaY96nVOpuANmGxyBnS-A25653IYjCOBOqGpNTLYdUALm75rICJmk9BkV29IHuS11I-gKFgviOkQnjC-QDMU6NMOEhDvOnNCgVPkvBeXh5KWnKDGGhT5qVnYHznM3i7qGh_bvJ3Vv2uGBA [scope] => GOOGLE [types] => Array ( [0] => casino [1] => spa [2] => lodging [3] => restaurant [4] => food [5] => point_of_interest [6] => establishment ) [vicinity] => 80 Pyrmont Street, Pyrmont ) [17] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.867978 [lng] => 151.195171 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.866802919708 [lng] => 151.19609848029 ) [southwest] => Array ( [lat] => -33.869500880291 [lng] => 151.19340051971 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png [id] => 7d4beb38f8a160ca1758002b846afbf713cfe432 [name] => Vantage, The Star [opening_hours] => Array ( [open_now] => 1 [weekday_text] => Array ( ) ) [photos] => Array ( [0] => Array ( [height] => 3024 [html_attributions] => Array ( [0] => Neek Woman ) [photo_reference] => CmRaAAAA09pV6Ak9le5QPb1Djz9eCeBAK1MaBhTjF_XWBw-OdOwDP3xY-vHGjnC7RK8lRUd6qZ6OFccb3woPGGZxL7C4v-MVThoMqRfbYVaET-GQoN6OgFFFXtluaR-fYSj2k_IFEhCPSwX1E1O_Bb2Pz49_GYvUGhSr70Gmld6MWqoTdMkgm00mkl4S4w [width] => 3024 ) ) [place_id] => ChIJ2fIN-TauEmsRj8cqw1d-YI0 [rating] => 4 [reference] => CmRSAAAALWJKfuthS5deCrLk_fBqSMVa_cXG5uBPDFrF5ifHvitgFsdOfhkzgW4-58x6qfRdm2m5kJE950tJ-z7w6V3K7eDqF8P00O-gJIfWQg6agRh3L-5WZdKvgqfMZd6JLmfOEhCNSI1TqwbYLWWT-xTs2mLHGhTeOfVM-lR9v9jMlJvPa7pqk_aucg [scope] => GOOGLE [types] => Array ( [0] => restaurant [1] => casino [2] => food [3] => point_of_interest [4] => establishment ) [vicinity] => 80 Pyrmont Street, Pyrmont ) [18] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.8669724 [lng] => 151.1944552 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.865714269709 [lng] => 151.19589088029 ) [southwest] => Array ( [lat] => -33.868412230291 [lng] => 151.19319291971 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png [id] => dd8ce7e0a6f502325a4a5f45abfa2e1dc3784e07 [name] => Ezy mart pyrmont [opening_hours] => Array ( [open_now] => 1 [weekday_text] => Array ( ) ) [photos] => Array ( [0] => Array ( [height] => 899 [html_attributions] => Array ( [0] => Ezy mart pyrmont ) [photo_reference] => CmRZAAAAxwL2stPwv920nCiDOd3qm8bUIZZ3DAolWumYyMOpjKSjo6kYnO_YDoHzWFICKERPnIPucrIbdOrP8oY06LKBkaVLWX9UwPiMugbCcpAt5bZjT2PDhnwT8S4fleleT4pFEhC47nMqVf_XsAOtVVK0quhkGhR4_lUCOwYmhQib9FhrxKpdTHWA8A [width] => 900 ) ) [place_id] => ChIJV37FXDauEmsRL6aHZGJ7Q04 [reference] => CmRRAAAAtFUKLoOXju5F3e6L_dVsuKxI2jeXsraJOdU9Ilzg2U66aJb-TSB9OacxmgovqGQhb6I9nUOIQnUmDdgJsPEaziy9xf7ztkNKMj1VemSjzLeFnF95SB0bUAyu0L25Fj-4EhC1FPac5lMJy-FmkPWdm5EyGhSlBzIOA3oqBAiszlxbCL7BTy4IRw [scope] => GOOGLE [types] => Array ( [0] => convenience_store [1] => food [2] => store [3] => point_of_interest [4] => establishment ) [vicinity] => 1/2 Jones Bay Road, Pyrmont ) [19] => Array ( [geometry] => Array ( [location] => Array ( [lat] => -33.868028 [lng] => 151.19521 ) [viewport] => Array ( [northeast] => Array ( [lat] => -33.866679019708 [lng] => 151.19655898029 ) [southwest] => Array ( [lat] => -33.869376980291 [lng] => 151.19386101971 ) ) ) [icon] => https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png [id] => ba9494fbebdb7e40f9a646aab93379212763195c [name] => Fat Noodle [opening_hours] => Array ( [open_now] => 1 [weekday_text] => Array ( ) ) [photos] => Array ( [0] => Array ( [height] => 469 [html_attributions] => Array ( [0] => Fat Noodle ) [photo_reference] => CmRaAAAA0zMsUGM3z6dLIJmGiOcA39rcUNhUSJJlNQAAqqEbQzOiWhTeYAAdfJsjB1aBIHtDysC3CJwyHxKXmpXweRBv2G0-60FZVdawXVRiTyA2KTnG-2qgAm6h9k2FfDIzhsUIEhB2XzrAKGRuxOEnCowpQ8i1GhT-FQbOYvsdFgdhR6QdRwBsOKjIrA [width] => 750 ) ) [place_id] => ChIJ1-v38TauEmsROWultgmCYW0 [price_level] => 1 [rating] => 3.2 [reference] => CmRRAAAAlCa7aYxV6SgfJZII_AqswB88Oo2q0--NWOibat1k2OuiuBenPkZ3IKbTB-d8iQg2AupBZxtaY4DuS27eE1YYNrlZ4kRk2G7IaY2j9uP73zpZNT7KS8ASw2ibmiCM9Y2hEhC2fpgHMn6HxPpTpDS-Sts2GhQB3NhqtuZKtouPwU4j0jXFlz0NzQ [scope] => GOOGLE [types] => Array ( [0] => restaurant [1] => food [2] => point_of_interest [3] => establishment ) [vicinity] => The Star, 80 Pyrmont Street, Pyrmont ) ) [status] => OK )
Checking at the structure of the json got from the call, to access the value you want to need to do like this.
$string = file_get_contents("https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&rankby=distance&types=food&key=AIzaSyCpOG91fkAzPTZiFq7G9HbLfbmVyW8Wgyc");
$json_a = json_decode($string, true);
print_r($json_a['results'][0]['name']));
Results is an array, and you want to access to its first element ([0]). From that first element you want to get the property name. And all the properties on the element are set inside an array, so you need to access it by the property name.
You were using the object operator (->) to access elements from an array, that's why it didn't work.
echo print_r(....); is in a way redundant. print_r it self prints result to output in similar way as echo and returns TRUE which is what you are printing.
From documentation:
mixed print_r ( mixed $expression [, bool $return = false ] )
When the return parameter is TRUE, this function will return a string. Otherwise, the return value is TRUE.
Either do not use echo or set the return param to TRUE.
echo print_r($array, true);

PHP - Looping through indexed JSON array

I have parsed and looped through JSON plenty of times. I have seemed to hit a wall with something I have not done before and I have been unable to find an answer and hopefully I am referencing this correctly. I have been really struggling with this.
With the code example below I can parse it and assign a varible to an object and get its value like so.
$result = json_decode($json, true);
print_r($result);
echo $result['response'][0]['report']['type'];
This is where the problem comes in and it starts with the array index and I am hoping I am referring to this correctly. Basically it is the [0] from above. I have never looped through something like that before and my research has come up empty. Normally I would do it like this.
$json = '{"success":true,"error":null,"response":[{"id":"59c30487db6be86b7f8b465a","loc":{"long":-90.45,"lat":43.43},"report":{"code":"R","type":"heavy rain","name":"Gillingham","detail":{"text":1,"rainIN":1,"rainMM":25.4},"reporter":"co-op observer","comments":"","timestamp":1505952240,"cat":"rain","dateTimeISO":"2017-09-20T19:04:00-05:00","datetime":"2017-09-20T19:04:00-05:00","wfo":"arx"},"place":{"name":"gillingham","state":"wi","county":"richland","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c302a9db6be82a758b46e8","loc":{"long":-90.93,"lat":43.32},"report":{"code":"R","type":"heavy rain","name":"Mount Sterling","detail":{"text":2.25,"rainIN":2.25,"rainMM":57.15},"reporter":"trained spotter","comments":"","timestamp":1505951760,"cat":"rain","dateTimeISO":"2017-09-20T18:56:00-05:00","datetime":"2017-09-20T18:56:00-05:00","wfo":"arx"},"place":{"name":"mount sterling","state":"wi","county":"crawford","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c30106db6be8146c8b4661","loc":{"long":-89.53,"lat":44.45},"report":{"code":"R","type":"heavy rain","name":"Plover","detail":{"text":1.05,"rainIN":1.05,"rainMM":26.67},"reporter":"co-op observer","comments":"One hour total rainfall. also measured a 49 mph wind gust at 511 pm. no hail.","timestamp":1505950800,"cat":"rain","dateTimeISO":"2017-09-20T18:40:00-05:00","datetime":"2017-09-20T18:40:00-05:00","wfo":"grb"},"place":{"name":"plover","state":"wi","county":"portage","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c3080adb6be8d5138b4654","loc":{"long":-89.33,"lat":44.37},"report":{"code":"H","type":"hail","name":"4 mi NNW Blaine","detail":{"text":0.88,"hailIN":0.88,"hailMM":22.35},"reporter":"trained spotter","comments":"Nickel size hail and heavy rain. 3.25 to 3.49 inches in the past 2 hours.","timestamp":1505950680,"cat":"hail","dateTimeISO":"2017-09-20T18:38:00-05:00","datetime":"2017-09-20T18:38:00-05:00","wfo":"grb"},"place":{"name":"blaine","state":"wi","county":"portage","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c30106db6be8146c8b4660","loc":{"long":-90.93,"lat":43.32},"report":{"code":"H","type":"hail","name":"Mount Sterling","detail":{"text":0.75,"hailIN":0.75,"hailMM":19.05},"reporter":"trained spotter","comments":"Hail ranged from 1\/2 to 3\/4 inch.","timestamp":1505950200,"cat":"hail","dateTimeISO":"2017-09-20T18:30:00-05:00","datetime":"2017-09-20T18:30:00-05:00","wfo":"arx"},"place":{"name":"mount sterling","state":"wi","county":"crawford","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c2fd80db6be844598b466d","loc":{"long":-89.77,"lat":44.21},"report":{"code":"R","type":"heavy rain","name":"6 mi ESE New Rome","detail":{"text":4,"rainIN":4,"rainMM":101.6},"reporter":"trained spotter","comments":"","timestamp":1505949840,"cat":"rain","dateTimeISO":"2017-09-20T18:24:00-05:00","datetime":"2017-09-20T18:24:00-05:00","wfo":"arx"},"place":{"name":"rome","state":"wi","county":"adams","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c2f678db6be898338b4673","loc":{"long":-89.3,"lat":44.46},"report":{"code":"H","type":"hail","name":"Amherst Junction","detail":{"text":1,"hailIN":1,"hailMM":25.4},"reporter":"trained spotter","comments":"","timestamp":1505948340,"cat":"hail","dateTimeISO":"2017-09-20T17:59:00-05:00","datetime":"2017-09-20T17:59:00-05:00","wfo":"grb"},"place":{"name":"amherst junction","state":"wi","county":"portage","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c2f2f6db6be8a2208b4672","loc":{"long":-89.4,"lat":44.26},"report":{"code":"H","type":"hail","name":"Almond","detail":{"text":2,"hailIN":2,"hailMM":50.8},"reporter":"trained spotter","comments":"Report via social media","timestamp":1505948160,"cat":"hail","dateTimeISO":"2017-09-20T17:56:00-05:00","datetime":"2017-09-20T17:56:00-05:00","wfo":"grb"},"place":{"name":"almond","state":"wi","county":"portage","country":"us"},"profile":{"tz":"America\/Chicago"}}]}';
$result = json_decode($json, true);
print_r($result);
foreach($result as report) {
$type = $report['report']['type'];
echo $type;
}
That would usually work in most cases but since this has the [0] and increases with each new entry I am not sure how to loop through that as my entry point is different that what I have dealt with before and have been unable to find any working examples for something like this. Not to mention I am not sure how to working ['response'] into the loop ether with those index keys.
Here is a decode example of the array and what I am working with.
Array ( [success] => 1 [error] => [response] => Array ( [0] => Array ( [id] => 59c30487db6be86b7f8b465a [loc] => Array ( [long] => -90.45 [lat] => 43.43 ) [report] => Array ( [code] => R [type] => heavy rain [name] => Gillingham [detail] => Array ( [text] => 1 [rainIN] => 1 [rainMM] => 25.4 ) [reporter] => co-op observer [comments] => [timestamp] => 1505952240 [cat] => rain [dateTimeISO] => 2017-09-20T19:04:00-05:00 [datetime] => 2017-09-20T19:04:00-05:00 [wfo] => arx ) [place] => Array ( [name] => gillingham [state] => wi [county] => richland [country] => us ) [profile] => Array ( [tz] => America/Chicago ) ) [1] => Array ( [id] => 59c302a9db6be82a758b46e8 [loc] => Array ( [long] => -90.93 [lat] => 43.32 ) [report] => Array ( [code] => R [type] => heavy rain [name] => Mount Sterling [detail] => Array ( [text] => 2.25 [rainIN] => 2.25 [rainMM] => 57.15 ) [reporter] => trained spotter [comments] => [timestamp] => 1505951760 [cat] => rain [dateTimeISO] => 2017-09-20T18:56:00-05:00 [datetime] => 2017-09-20T18:56:00-05:00 [wfo] => arx ) [place] => Array ( [name] => mount sterling [state] => wi [county] => crawford [country] => us ) [profile] => Array ( [tz] => America/Chicago ) ) [2] => Array ( [id] => 59c30106db6be8146c8b4661 [loc] => Array ( [long] => -89.53 [lat] => 44.45 ) [report] => Array ( [code] => R [type] => heavy rain [name] => Plover [detail] => Array ( [text] => 1.05 [rainIN] => 1.05 [rainMM] => 26.67 ) [reporter] => co-op observer [comments] => One hour total rainfall. also measured a 49 mph wind gust at 511 pm. no hail. [timestamp] => 1505950800 [cat] => rain [dateTimeISO] => 2017-09-20T18:40:00-05:00 [datetime] => 2017-09-20T18:40:00-05:00 [wfo] => grb ) [place] => Array ( [name] => plover [state] => wi [county] => portage [country] => us ) [profile] => Array ( [tz] => America/Chicago ) ) [3] => Array ( [id] => 59c3080adb6be8d5138b4654 [loc] => Array ( [long] => -89.33 [lat] => 44.37 ) [report] => Array ( [code] => H [type] => hail [name] => 4 mi NNW Blaine [detail] => Array ( [text] => 0.88 [hailIN] => 0.88 [hailMM] => 22.35 ) [reporter] => trained spotter [comments] => Nickel size hail and heavy rain. 3.25 to 3.49 inches in the past 2 hours. [timestamp] => 1505950680 [cat] => hail [dateTimeISO] => 2017-09-20T18:38:00-05:00 [datetime] => 2017-09-20T18:38:00-05:00 [wfo] => grb ) [place] => Array ( [name] => blaine [state] => wi [county] => portage [country] => us ) [profile] => Array ( [tz] => America/Chicago ) ) [4] => Array ( [id] => 59c30106db6be8146c8b4660 [loc] => Array ( [long] => -90.93 [lat] => 43.32 ) [report] => Array ( [code] => H [type] => hail [name] => Mount Sterling [detail] => Array ( [text] => 0.75 [hailIN] => 0.75 [hailMM] => 19.05 ) [reporter] => trained spotter [comments] => Hail ranged from 1/2 to 3/4 inch. [timestamp] => 1505950200 [cat] => hail [dateTimeISO] => 2017-09-20T18:30:00-05:00 [datetime] => 2017-09-20T18:30:00-05:00 [wfo] => arx ) [place] => Array ( [name] => mount sterling [state] => wi [county] => crawford [country] => us ) [profile] => Array ( [tz] => America/Chicago ) ) [5] => Array ( [id] => 59c2fd80db6be844598b466d [loc] => Array ( [long] => -89.77 [lat] => 44.21 ) [report] => Array ( [code] => R [type] => heavy rain [name] => 6 mi ESE New Rome [detail] => Array ( [text] => 4 [rainIN] => 4 [rainMM] => 101.6 ) [reporter] => trained spotter [comments] => [timestamp] => 1505949840 [cat] => rain [dateTimeISO] => 2017-09-20T18:24:00-05:00 [datetime] => 2017-09-20T18:24:00-05:00 [wfo] => arx ) [place] => Array ( [name] => rome [state] => wi [county] => adams [country] => us ) [profile] => Array ( [tz] => America/Chicago ) ) [6] => Array ( [id] => 59c2f678db6be898338b4673 [loc] => Array ( [long] => -89.3 [lat] => 44.46 ) [report] => Array ( [code] => H [type] => hail [name] => Amherst Junction [detail] => Array ( [text] => 1 [hailIN] => 1 [hailMM] => 25.4 ) [reporter] => trained spotter [comments] => [timestamp] => 1505948340 [cat] => hail [dateTimeISO] => 2017-09-20T17:59:00-05:00 [datetime] => 2017-09-20T17:59:00-05:00 [wfo] => grb ) [place] => Array ( [name] => amherst junction [state] => wi [county] => portage [country] => us ) [profile] => Array ( [tz] => America/Chicago ) ) [7] => Array ( [id] => 59c2f2f6db6be8a2208b4672 [loc] => Array ( [long] => -89.4 [lat] => 44.26 ) [report] => Array ( [code] => H [type] => hail [name] => Almond [detail] => Array ( [text] => 2 [hailIN] => 2 [hailMM] => 50.8 ) [reporter] => trained spotter [comments] => Report via social media [timestamp] => 1505948160 [cat] => hail [dateTimeISO] => 2017-09-20T17:56:00-05:00 [datetime] => 2017-09-20T17:56:00-05:00 [wfo] => grb ) [place] => Array ( [name] => almond [state] => wi [county] => portage [country] => us ) [profile] => Array ( [tz] => America/Chicago ) ) ) )
How would I loop through this with the Index key [#] and if I am referring to this incorrectly feel free to correct me.
You can use loop in $result["response"] as:
$json = '{"success":true,"error":null,"response":[{"id":"59c30487db6be86b7f8b465a","loc":{"long":-90.45,"lat":43.43},"report":{"code":"R","type":"heavy rain","name":"Gillingham","detail":{"text":1,"rainIN":1,"rainMM":25.4},"reporter":"co-op observer","comments":"","timestamp":1505952240,"cat":"rain","dateTimeISO":"2017-09-20T19:04:00-05:00","datetime":"2017-09-20T19:04:00-05:00","wfo":"arx"},"place":{"name":"gillingham","state":"wi","county":"richland","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c302a9db6be82a758b46e8","loc":{"long":-90.93,"lat":43.32},"report":{"code":"R","type":"heavy rain","name":"Mount Sterling","detail":{"text":2.25,"rainIN":2.25,"rainMM":57.15},"reporter":"trained spotter","comments":"","timestamp":1505951760,"cat":"rain","dateTimeISO":"2017-09-20T18:56:00-05:00","datetime":"2017-09-20T18:56:00-05:00","wfo":"arx"},"place":{"name":"mount sterling","state":"wi","county":"crawford","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c30106db6be8146c8b4661","loc":{"long":-89.53,"lat":44.45},"report":{"code":"R","type":"heavy rain","name":"Plover","detail":{"text":1.05,"rainIN":1.05,"rainMM":26.67},"reporter":"co-op observer","comments":"One hour total rainfall. also measured a 49 mph wind gust at 511 pm. no hail.","timestamp":1505950800,"cat":"rain","dateTimeISO":"2017-09-20T18:40:00-05:00","datetime":"2017-09-20T18:40:00-05:00","wfo":"grb"},"place":{"name":"plover","state":"wi","county":"portage","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c3080adb6be8d5138b4654","loc":{"long":-89.33,"lat":44.37},"report":{"code":"H","type":"hail","name":"4 mi NNW Blaine","detail":{"text":0.88,"hailIN":0.88,"hailMM":22.35},"reporter":"trained spotter","comments":"Nickel size hail and heavy rain. 3.25 to 3.49 inches in the past 2 hours.","timestamp":1505950680,"cat":"hail","dateTimeISO":"2017-09-20T18:38:00-05:00","datetime":"2017-09-20T18:38:00-05:00","wfo":"grb"},"place":{"name":"blaine","state":"wi","county":"portage","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c30106db6be8146c8b4660","loc":{"long":-90.93,"lat":43.32},"report":{"code":"H","type":"hail","name":"Mount Sterling","detail":{"text":0.75,"hailIN":0.75,"hailMM":19.05},"reporter":"trained spotter","comments":"Hail ranged from 1\/2 to 3\/4 inch.","timestamp":1505950200,"cat":"hail","dateTimeISO":"2017-09-20T18:30:00-05:00","datetime":"2017-09-20T18:30:00-05:00","wfo":"arx"},"place":{"name":"mount sterling","state":"wi","county":"crawford","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c2fd80db6be844598b466d","loc":{"long":-89.77,"lat":44.21},"report":{"code":"R","type":"heavy rain","name":"6 mi ESE New Rome","detail":{"text":4,"rainIN":4,"rainMM":101.6},"reporter":"trained spotter","comments":"","timestamp":1505949840,"cat":"rain","dateTimeISO":"2017-09-20T18:24:00-05:00","datetime":"2017-09-20T18:24:00-05:00","wfo":"arx"},"place":{"name":"rome","state":"wi","county":"adams","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c2f678db6be898338b4673","loc":{"long":-89.3,"lat":44.46},"report":{"code":"H","type":"hail","name":"Amherst Junction","detail":{"text":1,"hailIN":1,"hailMM":25.4},"reporter":"trained spotter","comments":"","timestamp":1505948340,"cat":"hail","dateTimeISO":"2017-09-20T17:59:00-05:00","datetime":"2017-09-20T17:59:00-05:00","wfo":"grb"},"place":{"name":"amherst junction","state":"wi","county":"portage","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c2f2f6db6be8a2208b4672","loc":{"long":-89.4,"lat":44.26},"report":{"code":"H","type":"hail","name":"Almond","detail":{"text":2,"hailIN":2,"hailMM":50.8},"reporter":"trained spotter","comments":"Report via social media","timestamp":1505948160,"cat":"hail","dateTimeISO":"2017-09-20T17:56:00-05:00","datetime":"2017-09-20T17:56:00-05:00","wfo":"grb"},"place":{"name":"almond","state":"wi","county":"portage","country":"us"},"profile":{"tz":"America\/Chicago"}}]}';
$result = json_decode($json, true);
foreach($result["response"] as $report) {
$type = $report['report']['type'];
echo $type;
}
Check the result here: http://sandbox.onlinephpfunctions.com/code/c7b55a67e649bdc8c35ec1ba8218747de1f8ac16

PHP Array issue , mulitple rows generated

I have an array that is fetched from database using some bindModel in cakephp.
finally now I want to change its format according to my liking to own.
here is the array contains.
Array
(
[0] => Array
(
[DriverLocation] => Array
(
[id] => 1
[dispensary_id] => 1
[driver_id] => 85
[zip_code_id] => 42
[created] => 2015-05-25 12:01:14
)
[ZipCode] => Array
(
[id] => 42
[province_id] => 3846
[city] => Rohtak
[zip_code] => 30215
[status] => active
)
[UserProfile] => Array
(
[first_name] => Arman
[last_name] => Kumar
)
)
[1] => Array
(
[DriverLocation] => Array
(
[id] => 2
[dispensary_id] => 1
[driver_id] => 85
[zip_code_id] => 43
[created] => 2015-05-25 12:01:14
)
[ZipCode] => Array
(
[id] => 43
[province_id] => 3846
[city] => Rohtak
[zip_code] => 15478
[status] => active
)
[UserProfile] => Array
(
[first_name] => Arman
[last_name] => Kumar
)
)
[2] => Array
(
[DriverLocation] => Array
(
[id] => 3
[dispensary_id] => 1
[driver_id] => 77
[zip_code_id] => 41
[created] => 2015-05-25 12:45:47
)
[ZipCode] => Array
(
[id] => 41
[province_id] => 3846
[city] => Malviya Vihar
[zip_code] => 12558
[status] => active
)
[UserProfile] => Array
(
[first_name] => Manish
[last_name] => Kumar
)
)
)
I want to convert or change its format something like as follows.
This will altered on the behalf of DriverLocation=>driver_id
Array
(
[0] => Array
(
[DriverLocation] => Array
(
[id] => 1
[dispensary_id] => 1
[driver_id] => 85
[zip_code_id] => 42
[created] => 2015-05-25 12:01:14
)
[ZipCode] => Array
(
[0] => Array
(
[id] => 42
[province_id] => 3846
[city] => Rohtak
[zip_code] => 30215
[status] => active
)
[1] => Array
(
[id] => 43
[province_id] => 3846
[city] => Rohtak
[zip_code] => 15478
[status] => active
)
)
[UserProfile] => Array
(
[first_name] => Arman
[last_name] => Kumar
)
)
[1] => Array
(
[DriverLocation] => Array
(
[id] => 3
[dispensary_id] => 1
[driver_id] => 77
[zip_code_id] => 41
[created] => 2015-05-25 12:45:47
)
[ZipCode] => Array
(
[id] => 41
[province_id] => 3846
[city] => Malviya Vihar
[zip_code] => 12558
[status] => active
)
[UserProfile] => Array
(
[first_name] => Manish
[last_name] => Kumar
)
)
)
You can use this loop:
foreach($ar as $k1=>$a1){
foreach($ar as $k2=>$a2){
if($k1 < $k2 && $a1["DriverLocation"]["driver_id"] == $a2["DriverLocation"]["driver_id"]){
$ar[$k1]["ZipCode"][] = $a2["ZipCode"];
if(isset($ar[$k1]["ZipCode"]["id"])){
$ar[$k1]["ZipCode"][] = array("id"=>$ar[$k1]["ZipCode"]["id"]);
unset($ar[$k1]["ZipCode"]["id"]);
}
unset($ar[$k2]);
}
}
}
Use array_push() function to make it
for sample example
<?php
$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a);
?>
output
Array ( [0] => red [1] => green [2] => blue [3] => yellow )

Getting the distance between two locations using Google Maps Api (JSON)

I want to get the distance between two locations using the Google API however this is the first time I hear about JSON and actually see its format. The variable $json_a should decode the JSON but when I try to get the distance I get "Notice: Undefined index: legs in C:\xampp\htdocs\app2\test.php on line 24". I added a link so you can see how the JSON structure looks like. I'll also want these locations ordered by distance and if you can provide any tips about how I could do that it would be great. Thanks! :)
PS: I know MySQL is depreciated, I'll look into MySQLi or PDO ASAP.
//$origin = $_COOKIE['origin'];
//$id=$_GET['id'];
$origin = "Bucuresti+Avrig+30";
$id = 3;
$query = ("SELECT * FROM locations WHERE category_id='$id'");
$result = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_array($result))
{
$address = $row['Judet'] . '+' . $row['Localitate'] . '+' . $row['Strada'] . '+' . $row['Numar'];
$address = str_replace(' ', '+', $address);
$fullurl = 'http://maps.googleapis.com/maps/api/directions/json?origin=' . $origin . '&destination=' . $address . '&alternatives=true&sensor=true';
$json_a = json_decode((file_get_contents($fullurl)),true);
echo $row['name'];
echo "<span class='badge'>" . $json_a['legs']['routes']['distance']['text'] . "</span></button>";
}
http://maps.googleapis.com/maps/api/directions/json?origin=Comanesti+Dimitrie+Ghica+13&destination=Brasov+1+Mai&alternatives=true&sensor=true
print_r shows me this:
Array ( [routes] => Array ( ) [status] => NOT_FOUND ) Array ( [routes] => Array ( [0] => Array ( [bounds] => Array ( [northeast] => Array ( [lat] => 44.4521487 [lng] => 26.1311305 ) [southwest] => Array ( [lat] => 44.4451446 [lng] => 26.1251047 ) ) [copyrights] => Map data ©2015 Google [legs] => Array ( [0] => Array ( [distance] => Array ( [text] => 1.2 km [value] => 1168 ) [duration] => Array ( [text] => 2 mins [value] => 137 ) [end_address] => Șoseaua Colentina 2, București, Romania [end_location] => Array ( [lat] => 44.4521487 [lng] => 26.1267169 ) [start_address] => Strada Avrig 30, București, Romania [start_location] => Array ( [lat] => 44.4451446 [lng] => 26.1311305 ) [steps] => Array ( [0] => Array ( [distance] => Array ( [text] => 0.4 km [value] => 397 ) [duration] => Array ( [text] => 1 min [value] => 42 ) [end_location] => Array ( [lat] => 44.4479077 [lng] => 26.1279682 ) [html_instructions] => Head northwest on Strada Avrig toward Aleea Avrig [polyline] => Array ( [points] => cugnGqvn~Ce#h#w#`AEDiArA_BnBm#r#QRsA~AeAlAaAjA ) [start_location] => Array ( [lat] => 44.4451446 [lng] => 26.1311305 ) [travel_mode] => DRIVING ) [1] => Array ( [distance] => Array ( [text] => 0.2 km [value] => 158 ) [duration] => Array ( [text] => 1 min [value] => 22 ) [end_location] => Array ( [lat] => 44.447097 [lng] => 26.1263385 ) [html_instructions] => Turn left onto Strada Chiristigiilor [maneuver] => turn-left [polyline] => Array ( [points] => mfhnGybn~CJTj#rAZv#Rh#f#nAPh# ) [start_location] => Array ( [lat] => 44.4479077 [lng] => 26.1279682 ) [travel_mode] => DRIVING ) [2] => Array ( [distance] => Array ( [text] => 0.3 km [value] => 282 ) [duration] => Array ( [text] => 1 min [value] => 32 ) [end_location] => Array ( [lat] => 44.4494197 [lng] => 26.1251047 ) [html_instructions] => Turn right onto Șoseaua Mihai Bravu [maneuver] => turn-right [polyline] => Array ( [points] => kahnGsxm~CiA#mAX[FGBMDWHEBc#Pa#T]TUPEBgBfB ) [start_location] => Array ( [lat] => 44.447097 [lng] => 26.1263385 ) [travel_mode] => DRIVING ) [3] => Array ( [distance] => Array ( [text] => 0.3 km [value] => 331 ) [duration] => Array ( [text] => 1 min [value] => 41 ) [end_location] => Array ( [lat] => 44.4521487 [lng] => 26.1267169 ) [html_instructions] => Turn right onto Șoseaua Colentina/DN2/E60/E85
Destination will be on the right
[maneuver] => turn-right [polyline] => Array ( [points] => {ohnG{pm~CYE[IoBk#oFeDYWYQq#[e#[ ) [start_location] => Array ( [lat] => 44.4494197 [lng] => 26.1251047 ) [travel_mode] => DRIVING ) ) [via_waypoint] => Array ( ) ) ) [overview_polyline] => Array ( [points] => cugnGqvn~CaL|MgCxCv#hBn#`Bx#xBiA#mAXc#JoAd#{A`AgBfBYEkCu#iG}DkAm#e#[ ) [summary] => Strada Avrig and Șoseaua Colentina/DN2/E60/E85 [warnings] => Array ( ) [waypoint_order] => Array ( ) ) [1] => Array ( [bounds] => Array ( [northeast] => Array ( [lat] => 44.4521487 [lng] => 26.131243 ) [southwest] => Array ( [lat] => 44.4438465 [lng] => 26.1251047 ) ) [copyrights] => Map data ©2015 Google [legs] => Array ( [0] => Array ( [distance] => Array ( [text] => 1.3 km [value] => 1282 ) [duration] => Array ( [text] => 3 mins [value] => 153 ) [end_address] => Șoseaua Colentina 2, București, Romania [end_location] => Array ( [lat] => 44.4521487 [lng] => 26.1267169 ) [start_address] => Strada Avrig 30, București, Romania [start_location] => Array ( [lat] => 44.4451446 [lng] => 26.1311305 ) [steps] => Array ( [0] => Array ( [distance] => Array ( [text] => 9 m [value] => 9 ) [duration] => Array ( [text] => 1 min [value] => 1 ) [end_location] => Array ( [lat] => 44.4450833 [lng] => 26.1312017 ) [html_instructions] => Head southeast on Strada Avrig [polyline] => Array ( [points] => cugnGqvn~CJM ) [start_location] => Array ( [lat] => 44.4451446 [lng] => 26.1311305 ) [travel_mode] => DRIVING ) [1] => Array ( [distance] => Array ( [text] => 0.2 km [value] => 209 ) [duration] => Array ( [text] => 1 min [value] => 45 ) [end_location] => Array ( [lat] => 44.4439255 [lng] => 26.1293794 ) [html_instructions] => Take the 1st right toward Șoseaua Mihai Bravu [polyline] => Array ( [points] => wtgnG_wn~CHCFAFAFAD#F#D#D#DBFH~#jCHRRj#BHZ`AJVPn# ) [start_location] => Array ( [lat] => 44.4450833 [lng] => 26.1312017 ) [travel_mode] => DRIVING ) [2] => Array ( [distance] => Array ( [text] => 0.7 km [value] => 733 ) [duration] => Array ( [text] => 1 min [value] => 66 ) [end_location] => Array ( [lat] => 44.4494197 [lng] => 26.1251047 ) [html_instructions] => Turn right onto Șoseaua Mihai Bravu [maneuver] => turn-right [polyline] => Array ( [points] => qmgnGskn~CNf#KPGL_#n#]j#m#|#e#h#[Za#b#eBrAqBxAIFk#Z_#R{#X{#RiA#mAX[FGBMDWHEBc#Pa#T]TUPEBgBfB ) [start_location] => Array ( [lat] => 44.4439255 [lng] => 26.1293794 ) [travel_mode] => DRIVING ) [3] => Array ( [distance] => Array ( [text] => 0.3 km [value] => 331 ) [duration] => Array ( [text] => 1 min [value] => 41 ) [end_location] => Array ( [lat] => 44.4521487 [lng] => 26.1267169 ) [html_instructions] => Turn right onto Șoseaua Colentina/DN2/E60/E85
Destination will be on the right
[maneuver] => turn-right [polyline] => Array ( [points] => {ohnG{pm~CYE[IoBk#oFeDYWYQq#[e#[ ) [start_location] => Array ( [lat] => 44.4494197 [lng] => 26.1251047 ) [travel_mode] => DRIVING ) ) [via_waypoint] => Array ( [0] => Array ( [location] => Array ( [lat] => 44.4445541 [lng] => 26.130791 ) [step_index] => 1 [step_interpolation] => 0.36662061075431 ) ) ) ) [overview_polyline] => Array ( [points] => cugnGqvn~CTQNCZBJDFH~#jC\~#^jA\fANf#KPg#|#kAhBaAdAa#b#eBrA{B`BkAn#wBl#iA#mAXc#Je#Ni#T_Aj#[TgBfBYEkCu#iG}DkAm#e#[ ) [summary] => Șoseaua Mihai Bravu and Șoseaua Colentina/DN2/E60/E85 [warnings] => Array ( ) [waypoint_order] => Array ( ) ) ) [status] => OK )
For finding the first value of routes, you can use below code :-
$json_a['routes'][0]['legs'][0]['distance']['text']
If you want to show all values then use foreach.
For sorting on values
function cmp($a, $b)
{
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
$a = array(3, 2, 5, 6, 1);
usort($a, "cmp");
foreach ($a as $key => $value) {
echo "$key: $value\n";
}

Categories