I am having trouble with retrieving values from a JSON being returned when getting place details from the Google Maps API. Here is the specific JSON I would like to parse:
{
"html_attributions" : [],
"result" : {
"address_components" : [
{
"long_name" : "48",
"short_name" : "48",
"types" : [ "street_number" ]
},
{
"long_name" : "Pirrama Road",
"short_name" : "Pirrama Road",
"types" : [ "route" ]
},
{
"long_name" : "Pyrmont",
"short_name" : "Pyrmont",
"types" : [ "locality", "political" ]
},
{
"long_name" : "NSW",
"short_name" : "NSW",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "AU",
"short_name" : "AU",
"types" : [ "country", "political" ]
},
{
"long_name" : "2009",
"short_name" : "2009",
"types" : [ "postal_code" ]
}
],
"adr_address" : "5,
\u003cspan class=\"street-address\"\u003e48 Pirrama Rd\u003c/span\u003e,
\u003cspan class=\"locality\"\u003ePyrmont\u003c/span\u003e
\u003cspan class=\"region\"\u003eNSW\u003c/span\u003e
\u003cspan class=\"postal-code\"\u003e2009\u003c/span\u003e,
\u003cspan class=\"country-name\"\u003eAustralia\u003c/span\u003e",
"formatted_address" : "48 Pirrama Road, Pyrmont NSW, Australia",
"formatted_phone_number" : "(02) 9374 4000",
"geometry" : {
"location" : {
"lat" : -33.8669710,
"lng" : 151.1958750
},
"viewport" : {
"northeast" : {
"lat" : -33.8665053,
"lng" : 151.1960371
},
"southwest" : {
"lat" : -33.8669293,
"lng" : 151.1952183
}
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "4f89212bf76dde31f092cfc14d7506555d85b5c7",
"international_phone_number" : "+61 2 9374 4000",
"name" : "Google Sydney",
"place_id" : "ChIJN1t_tDeuEmsRUsoyG83frY4",
"scope" : "GOOGLE",
"alt_ids" : [
{
"place_id" : "D9iJyWEHuEmuEmsRm9hTkapTCrk",
"scope" : "APP"
}
],
"rating" : 4.70,
"reference" : "CnRsAAAA98C4wD-VFvzGq-KHVEFhlHuy1TD1W6UYZw7KjuvfVsKMRZkbCVBVDxXFOOCM108n9PuJMJxeAxix3WB6B16c1p2bY1ZQyOrcu1d9247xQhUmPgYjN37JMo5QBsWipTsnoIZA9yAzA-0pnxFM6yAcDhIQbU0z05f3xD3m9NQnhEDjvBoUw-BdcocVpXzKFcnMXUpf-nkyF1w",
"reviews" : [
{
"aspects" : [
{
"rating" : 3,
"type" : "quality"
}
],
"author_name" : "Simon Bengtsson",
"author_url" : "https://plus.google.com/104675092887960962573",
"language" : "en",
"rating" : 5,
"text" : "Just went inside to have a look at Google. Amazing.",
"time" : 1338440552869
},
{
"aspects" : [
{
"rating" : 3,
"type" : "quality"
}
],
"author_name" : "Felix Rauch Valenti",
"author_url" : "https://plus.google.com/103291556674373289857",
"language" : "en",
"rating" : 5,
"text" : "Best place to work :-)",
"time" : 1338411244325
},
{
"aspects" : [
{
"rating" : 3,
"type" : "quality"
}
],
"author_name" : "Chris",
"language" : "en",
"rating" : 5,
"text" : "Great place to work, always lots of free food!",
"time" : 1330467089039
}
],
"types" : [ "establishment" ],
"url" : "http://maps.google.com/maps/place?cid=10281119596374313554",
"vicinity" : "48 Pirrama Road, Pyrmont",
"website" : "http://www.google.com.au/"
},
"status" : "OK"
}
I am trying to retrieve data particularly from the name and formatted_address keys.
I have tried the following (with the json file above being named practice.json for example):
$jsondata = file_get_contents("practice.json");
$fileDecode = json_decode($jsondata);
echo $fileDecode->result->name;
But I am not able to get any output. Any assistance is much appreciated!
EDIT: This is a second json taken directly from Google Maps API, also not able to retrieve data from:
{
"html_attributions" : [],
"result" : {
"address_components" : [
{
"long_name" : "Al Olaya",
"short_name" : "Al Olaya",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "Riyadh",
"short_name" : "Riyadh",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Riyadh Province",
"short_name" : "Riyadh Province",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Saudi Arabia",
"short_name" : "SA",
"types" : [ "country", "political" ]
}
],
"adr_address" : "\u003cspan class=\"region\"\u003eAl Olaya\u003c/span\u003e, \u003cspan class=\"locality\"\u003eRiyadh\u003c/span\u003e \u003cspan class=\"country-name\"\u003eSaudi Arabia\u003c/span\u003e",
"formatted_address" : "Al Olaya, Riyadh Saudi Arabia",
"geometry" : {
"location" : {
"lat" : 24.6955386,
"lng" : 46.6850215
},
"viewport" : {
"northeast" : {
"lat" : 24.716452,
"lng" : 46.7034227
},
"southwest" : {
"lat" : 24.6681904,
"lng" : 46.6635758
}
}
},
"icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
"id" : "04d055d65120a302eeedbdce0e8b570cbb2026ed",
"name" : "Al Olaya",
"photos" : [
{
"height" : 1667,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/117088877528560272504/photos\"\u003eHussam abdul nour\u003c/a\u003e"
],
"photo_reference" : "CoQBdwAAALzzt2l80CpXQn_Pg2DI9bOxynSSRyNQw4lFes4CL4SNmfl_4a-o1zgmpoK8O-1Jp8Rh08XQg3nlx0mO0JW3-h8xnIkV6D42SEjM-wx6t_ha3IclwpJC6hEtZzEcWXWcCsNyiuvDZWik-SYo9Ae2z3RsLzfRREumciDDmQflcpioEhCTeD-0tjzVt0hFAD_so7uqGhTwRrigrOfOa04qCcRhDN24v2J2kA",
"width" : 2500
},
{
"height" : 2448,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/101149623891738396593/photos\"\u003eCompssaiŋoɑter\u003c/a\u003e"
],
"photo_reference" : "CoQBdwAAANsdQ2VD-tuohl7udmcLHReWz87P9ZoZTM_L8krBB4jaAYlPQc4tdygrUnZ6fXEUvF4kwFFrFUswRf9ObP8QqdplDY93Nf_dg8pcloVx0iRpOwPW7yv18WV1o1vB0vvF4HytfiK2KWymPqrhZ8NZ-8RYN9rc_idToVoByvYHqFtWEhAXkX47Lb2vcBIVdZjG9PEaGhSsenjXTjqzphIdN-oMeG3zKotU3A",
"width" : 3264
},
{
"height" : 960,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/110505115125440575608/photos\"\u003eam an\u003c/a\u003e"
],
"photo_reference" : "CoQBdwAAALFDIa3AeXB62bvuL9V8s79PJA5-f6cW4j2FJvZ8RAcekWkeLDh0b_T13kzL0o7jPbpppfqIi4DjYueD20c84d3Nr9FoS8-YM1YGiqgITw_chOacDIkYlgNpGVA1Lh6DQKJDqtAl6Rr8nzWATUDRjboLRjx5rZFhMmpUEg2Z1nLwEhBalGiaH7g3t1CnkRWEekJQGhREdC4UgB_NfivkK-Sb0qyXViCEWw",
"width" : 1280
},
{
"height" : 3024,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/102067930211195473360/photos\"\u003eف العلي\u003c/a\u003e"
],
"photo_reference" : "CoQBdwAAAH4vqs9NYfw3LhRyzS0JUjU0DMLwJUsv7KLYQ4rk74U0pG3yxHPaucQ3o-ehc5tGa4sg6dKH0gCSmPYBjRbuPN4qihitm2jgWCVvsHi4kG0RmSX_95S-WvQ_SC-EBUNzLft8vkAhVY_WzhWv_tnNdYcE6eFj3td0q01IcTktz6rmEhC_M8Ut3tYf3xFHyf74YtO8GhTEUse64pmo0nzXJViSWS3kIgXrGQ",
"width" : 4032
},
{
"height" : 1024,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/113204813794170333862/photos\"\u003eNadine Ellahib\u003c/a\u003e"
],
"photo_reference" : "CoQBdwAAAHe5--s72SnRkwOdrUh7Jjd00uyaHqblIOe0JA8cHuSKfKfWvkbFLiQYgLyEzJ9uPTxBv8owv_mYlP3QPypdVi2Z9KWzhlDO9-9uMK6uG22rdDythbLAsXmqtSo30Youd1fYUDI5TyGAk4_GZLBpab79r54mwbE46T7yjooRMOvFEhAeEWrwH9dLg_fX96wFZG7CGhSsaVTzbFBaGb2DMyuTWVtYQIXf1g",
"width" : 1280
},
{
"height" : 960,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/115381450017292739172/photos\"\u003ebayt rafal\u003c/a\u003e"
],
"photo_reference" : "CoQBdwAAAPLiE2rnNB1oKu9sGeppOBkkSFaNMgIqHICb-dJNxvNYNdsSRRnpvIejKWWhqBlWMqCWM4ckOvaQ-QpoxsUyrwUuJxi0IQ1ApnpWBqwdYzQAJm38rnh-afPsdY2V7jPnlGnPRs1hKKNbnP1uVnvdmBi-3_Pl5X7DtX-na3Vgt7-vEhDmDvgjrRqDcDsgdWbN9wFdGhSNt9vXR-DDKfCsJpxXEYIm_wQz2A",
"width" : 1280
},
{
"height" : 4912,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/117800400423280078042/photos\"\u003eAli Ali\u003c/a\u003e"
],
"photo_reference" : "CoQBdwAAAGXi7GJE3Ms5sI6ODIAW6EdUETVTK3USk5Y5_eUyrh_T2jgrHqMr1oYvPy8GWhdn5k9HB71n-og2wVPGISxds9yWvM_GG_qOYJMT0FtBZHKl9PMRyyJMrPQPpenpz_Y6qQ1ExegDrlP_9vCSB-i7SL5H-0_Bd8FkjAobhnLakhleEhC288Fky3Mzp3z640Xu1YgBGhQXZYU-KJS7yFB3bd5Pa_ah4HtHyw",
"width" : 7360
},
{
"height" : 460,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/111425848650488622103/photos\"\u003eMahmud Hamdy\u003c/a\u003e"
],
"photo_reference" : "CoQBdwAAADPfaci3PMLYTTE8KneDYP4nezEamTSDtZjjnE-dU7TlHUipryBG-5_kf5efH8CZp97utlUwJDLyC72gXC7FVTo-m6OOKL-nUTXvTnDuVBfI-0O5r1Lk7xI00-PwiRLy8ZjnzrSKY8wAUwb5ik3K5BMqArhGQTnXb40BXkA5ao9DEhCUPocB0i3SD5K-13UZgqQ-GhQJu2b1lOsZWwRdU5dZBnEMOXNmeQ",
"width" : 1024
},
{
"height" : 2976,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/116747951557154073650/photos\"\u003eAbo Osamah\u003c/a\u003e"
],
"photo_reference" : "CoQBdwAAAPuy2BrljeilT3ZVivEyn0fxNm4aS1OBMIui1ty57-CHk0D_fUQ9B9PQqL7dVhvGE-GFkNBPpsBz8N1Hu0NIMfcjv0x8T7OBPY5vKEGHcG3LDx2xEzy7nU214wqK7HAlj06b6704CoHtQ7aTVZeVD6H910yv9ARCVbdTWKMuym05EhCuQ44VEJtjjsHcCIAZCY1qGhSUcNgdgsMUF3LnahV1O9Cv5q3B8w",
"width" : 3968
},
{
"height" : 1440,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/105026382392779621412/photos\"\u003eSalahuddin saleem\u003c/a\u003e"
],
"photo_reference" : "CoQBdwAAANsGbEK1sqRiTVRbDczYQBpN7skqGJlSZsXYgwEeqPLCIjStMIxNjNli2wA-oBzkRjQk4Jy_yeGMlXizjEw1CXSbgMDfvNz-z8MpHG-bwAH9SHbVHZa6xnyVzzEpTLfyW2BxdeWjQVBRqJrZLin1TrtLlCqQpOlkzcy5SBwL8NwvEhCZVoZzlsDAW4Bqam01E-4eGhSx4Yvqn96VWxLmDc-OpDhj48ByCA",
"width" : 2560
}
],
"place_id" : "ChIJF7QkuDsDLz4R0rJ4SsxFl9w",
"reference" : "CmRbAAAAwmMaielR1fUd5dDwGvebSvjTkTqtv_KiDPFdKzMneku63kc0V4Uqe15jJRmYsuxh8Mv33fef494zUk7Ip3rl5g7DSHgh7EU4XfG9VhYkMPGgURb08oXLDlBYi1xJTd9OEhDcvi55Sziz3I-IjojRCPrvGhQs-g47WwHHvR36JPrkmUxoM3k_ww",
"scope" : "GOOGLE",
"types" : [ "sublocality_level_1", "sublocality", "political" ],
"url" : "https://maps.google.com/?q=Al+Olaya,+Riyadh+Saudi+Arabia&ftid=0x3e2f033bb824b417:0xdc9745cc4a78b2d2",
"utc_offset" : 180,
"vicinity" : "Al Olaya"
},
"status" : "OK"
}
Error in these lines.. Correct Json file..
used this link to get error in practice.json files http://jsonlint.com/
"adr_address" : "5,
\u003cspan class=\"street-address\"\u003e48 Pirrama Rd\u003c/span\u003e,
\u003cspan class=\"locality\"\u003ePyrmont\u003c/span\u003e
\u003cspan class=\"region\"\u003eNSW\u003c/span\u003e
\u003cspan class=\"postal-code\"\u003e2009\u003c/span\u003e,
\u003cspan class=\"country-name\"\u003eAustralia\u003c/span\u003e",
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have a JSON file which contains reviews that I want to access. it looks like this:
{
"html_attributions" : [],
"result" : {
"address_components" : [
{
"long_name" : "Antrim Road",
"short_name" : "Antrim Rd",
"types" : [ "route" ]
},
{
"long_name" : "Belfast",
"short_name" : "Belfast",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Newtownabbey",
"short_name" : "Newtownabbey",
"types" : [ "postal_town" ]
},
{
"long_name" : "Belfast",
"short_name" : "Belfast",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Northern Ireland",
"short_name" : "Northern Ireland",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
},
{
"long_name" : "BT36 7PN",
"short_name" : "BT36 7PN",
"types" : [ "postal_code" ]
}
],
"adr_address" : "\u003cspan class=\"street-address\"\u003eAntrim Road\u003c/span\u003e, \u003cspan class=\"extended-address\"\u003eBelfast\u003c/span\u003e, \u003cspan class=\"locality\"\u003eNewtownabbey\u003c/span\u003e \u003cspan class=\"postal-code\"\u003eBT36 7PN\u003c/span\u003e, \u003cspan class=\"country-name\"\u003eUK\u003c/span\u003e",
"business_status" : "OPERATIONAL",
"formatted_address" : "Antrim Rd, Belfast, Newtownabbey BT36 7PN, UK",
"formatted_phone_number" : "028 9077 6277",
"geometry" : {
"location" : {
"lat" : 54.6577821,
"lng" : -5.943665699999999
},
"viewport" : {
"northeast" : {
"lat" : 54.65913108029149,
"lng" : -5.942316719708497
},
"southwest" : {
"lat" : 54.6564331197085,
"lng" : -5.945014680291502
}
}
},
"icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/generic_business-71.png",
"international_phone_number" : "+44 28 9077 6277",
"name" : "Belfast Zoo",
"photos" : [
{
"height" : 1243,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/111863060002111236588\"\u003eBelfast Zoo\u003c/a\u003e"
],
"photo_reference" : "ATtYBwJTIiPI3VznHujO9vTlzCagiH0Ak6dcoeZZsuNi8aIR1HzkLKmDbAEq-8ctO68YLbIuAqJeiu9X1tyhC6mKjcns0wGPRtTDqbhFTyle7LPE2oRa5Oda2FXzSDf8nrC1WR44YkdyPUhZUnN431HztHIFkbJF_eEYGvQffmflID9TqALU",
"width" : 1776
},
{
"height" : 2988,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/100936618118839321951\"\u003eAmanda Lewis\u003c/a\u003e"
],
"photo_reference" : "ATtYBwI55L16mowjExP5cOFV2wKThreGKXYT_1t6jK4JkGcUap94VNPF9dN2OniYKmi9YGDNqALwY4-6MWZoxm6-zyDNxog8g7c8UwD9UpXVrivmkfCpuY9MguXFiJlRVw9noyV6QMI-TO2bEVbvd7wQgnXn1Eaig_puaCDcylTcZ8nrOJAp",
"width" : 5312
},
{
"height" : 2988,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/116390912159770135890\"\u003eBeti Na\u003c/a\u003e"
],
"photo_reference" : "ATtYBwJQ94yxuoAhN3MTMbf7DOdTgpJAYUoWbHFQW_i_uh6eQqdSdWzAiy5Du48Lm3bDnpWSPI4daH3AHem_XweVtP4MSaIPQZe4ozYWbEX1RPrKtDRMGu3kmqsMSCS84kyMLTlDOYpGuC7w4gqEzaqcg5DbIXiPoIAjXzFxTPCV1OdeRm8Q",
"width" : 5312
},
{
"height" : 9248,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/106644795451552578979\"\u003eMatthew mcmullan\u003c/a\u003e"
],
"photo_reference" : "ATtYBwJDE3wE13EJptVWwrlPe_-n9HnwqU8Hs5y7HpWm_5hqMXvmo3AaZzyZsoZTfyV1M69dFgZnsJrP9iyiarwEuXYHPOA4ogvr0j-pn5XPk4nBdVrnPtbgPh0JYoym62ac3y1I33XBvwc-1eXk_MJbQJ_UvIcr2c6OmDLZSq0r4ESN6y7J",
"width" : 6936
},
{
"height" : 2988,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/116390912159770135890\"\u003eBeti Na\u003c/a\u003e"
],
"photo_reference" : "ATtYBwJbAFdJSWGNrd6uJb6yf_oM4Rfn2fkbewqz2lHTWl0buXcQ78YbBiJ8Ta_umMEo3XrFOS599cO-x01OvS83tiiIdlRXu8NOWcLxSpZmYuq41kG2KVyWz1MOJ9YjqZbLdIzADlZ641EipOAcmmZv-TBdbzHwHC7S3wrYrdaa67p7rvTR",
"width" : 5312
},
{
"height" : 3823,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/116284097622909280304\"\u003eBrian Cunningham\u003c/a\u003e"
],
"photo_reference" : "ATtYBwL7Z-RSxZ6pZ-i4XmKKgGNCazKNVw2bITY5sN1FFlDpbF01rF21rlpi54WJCHf23se8yAertLRQb-kAq2PLYB9ymsiQnrveFL-GttZjyGNwvuXpos-g9Lm0J58_I32Mr6YcgS5wfJIp74DQv2PyE6Wjo8NUrVyj5sLctb1FpVS92PuJ",
"width" : 5735
},
{
"height" : 4000,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/109005734355336105418\"\u003eWilliam Young\u003c/a\u003e"
],
"photo_reference" : "ATtYBwKPd5mh06H7KLngtPKQPfU8Wfq2BD08-iL51gJqhZea1zrZAK-eT6fAM3X8qde9TkciaMCgYo64_Ik1MEIIBAzDBYOt_2ug0pkeg3BWM7TNKyFu6k__qcImA2CFJE1AMsiKitlonSApY4w1t53D75rO0xEpmmL8cMRSIEt7YNrGhVSb",
"width" : 2723
},
{
"height" : 3024,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/100719597211687837516\"\u003eJonathan Kelly\u003c/a\u003e"
],
"photo_reference" : "ATtYBwJCyTy5R0N7jNCmnggCXgn70Xgnws1KBXocRpAfjKBjEVZszRMQpQ9-ZZxZcPdaWzDKDtUhnEHyavQio3u3etvoc5q6RFaLGhOA7SK58wE84pbMEz5YazqArwuEA0jDBNEctRMO9J9DTYDhkt4jfrbTAke1-AfJTZFkukLwXjpA_oEA",
"width" : 4032
},
{
"height" : 3024,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/104554106193648036788\"\u003eAjay Jose\u003c/a\u003e"
],
"photo_reference" : "ATtYBwKrV-cN2oWcr0iD65VKbfYTQLy1vLDPVs2YnbEtFq3Ne5KH16ooiWCD18Nc6VjZZ1Jr5Lt54yQtbRN7XIr-78e6YwtXrV1ue_0aHahqpv17YfscGFzfQ7X5SW0gEU19dbVSX0oC3BGdzAbHmjO04OXNS2vu6u1HDKT67mWlRTkSYovM",
"width" : 4032
},
{
"height" : 3024,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/100719597211687837516\"\u003eJonathan Kelly\u003c/a\u003e"
],
"photo_reference" : "ATtYBwKgM41e8PE5cKx_nAgU_fW4XHJeIy-StMar51N7ZbVjByUpfiqS7FbtQ-gWkGrRt9vNqOcsb9BysvgErNi-Ai0TEcQ2pLv5qOuyQ1S2uBWTWEksuV6wzDcXYNKTLa7uE-_H_rBH2pKoKCWl0meTtuq55HuNuFOkN6OEQESk-DN4ZzQH",
"width" : 4032
}
],
"place_id" : "ChIJ88iCe5inYUgRdeXmfCtuvu4",
"plus_code" : {
"compound_code" : "M354+4G Belfast, UK",
"global_code" : "9C6PM354+4G"
},
"rating" : 4.3,
"reference" : "ChIJ88iCe5inYUgRdeXmfCtuvu4",
"reviews" : [
{
"author_name" : "Karen Hoad",
"author_url" : "https://www.google.com/maps/contrib/103129952317107981705/reviews",
"language" : "en",
"profile_photo_url" : "https://lh6.googleusercontent.com/-OkGM8wAyHz8/AAAAAAAAAAI/AAAAAAAAAAA/AMZuucn55YdMQZ1BX6ods-S34cieMQODrg/s128-c0x00000000-cc-rp-mo-ba5/photo.jpg",
"rating" : 4,
"relative_time_description" : "6 months ago",
"text" : "Great place just not good when raining as the animals stay in",
"time" : 1599236033
},
{
"author_name" : "John Lee Ashworth",
"author_url" : "https://www.google.com/maps/contrib/116452096322094109908/reviews",
"language" : "en",
"profile_photo_url" : "https://lh3.googleusercontent.com/-_ZpK7cdK1NI/AAAAAAAAAAI/AAAAAAAAAAA/AMZuuclmcdFSQNilc5MfhJ3m8rRqzAXO_A/s128-c0x00000000-cc-rp-mo/photo.jpg",
"rating" : 3,
"relative_time_description" : "6 months ago",
"text" : "Love zoos, love animals, hate nothing but steep hills though",
"time" : 1598880406
},
{
"author_name" : "Clare",
"author_url" : "https://www.google.com/maps/contrib/116989491220973031798/reviews",
"language" : "en",
"profile_photo_url" : "https://lh3.googleusercontent.com/a-/AOh14GjK9VEovk8XRG41K9vVtu0SKRIHQdXp6Gb79opr_g=s128-c0x00000000-cc-rp-mo-ba2",
"rating" : 2,
"relative_time_description" : "5 months ago",
"text" : "Very little animals to see.",
"time" : 1600628749
},
{
"author_name" : "Tammy 102",
"author_url" : "https://www.google.com/maps/contrib/111672456708879530776/reviews",
"language" : "en",
"profile_photo_url" : "https://lh5.googleusercontent.com/-ngjoEXxeE8Q/AAAAAAAAAAI/AAAAAAAAAAA/AMZuucnTOkOTeq8ecWLEVbSs7UBnsUPmSQ/s128-c0x00000000-cc-rp-mo-ba3/photo.jpg",
"rating" : 5,
"relative_time_description" : "6 months ago",
"text" : "To check prices",
"time" : 1600610117
},
{
"author_name" : "Mark Woonton",
"author_url" : "https://www.google.com/maps/contrib/106800516570033737478/reviews",
"language" : "en",
"profile_photo_url" : "https://lh3.googleusercontent.com/a-/AOh14GgnYZybrP-MTC4U45agdIlXMIFDIuH1v6roRuJAWQ=s128-c0x00000000-cc-rp-mo",
"rating" : 5,
"relative_time_description" : "6 months ago",
"text" : "Amazing zoo, so much to see, a great day out",
"time" : 1599993064
}
],
"types" : [ "zoo", "tourist_attraction", "point_of_interest", "establishment" ],
"url" : "https://maps.google.com/?cid=17203308759660029301",
"user_ratings_total" : 2714,
"utc_offset" : 0,
"vicinity" : "Antrim Road, Belfast",
"website" : "http://www.belfastzoo.co.uk/"
},
"status" : "OK"
}
it continues, but I want to loop through the number of reviews there are, so I can grab all of them and post it onto my website.
I am getting the count like this :
$json = json_decode($place_id, true);
$reviewCount = count($json);
and then I have my for loop:
for ($i = 0; $i < $reviewCount; $i++){
...
}
What I've realised is my count always returns 3. I used var_dump to see what it actually counts, and its not the reviews.
I'm not sure how I would actually count the number of reviews, so I'm stuck as this point.
Any help or even info would be massively appriciated, thanks!
(edited to provide the entire json file)
OK, seeing that you updated your question, you will need to count your reviews like this:
$objFromJson = json_decode($jsonString, true);
$reviews=$objFromJson["result"]["reviews"];
$reviewCount = count($reviews); // 5
echo "The count is $reviewCount, here are the individual reviews:\n\n";
foreach ($reviews as $rev) echo $rev["author_name"].": ".$rev["text"]."\n";
I also changed your for loop into a foreach loop, see my (updated) demo here: https://rextester.com/JQEU23562
output:
The count is 5, here are the individual reviews:
Karen Hoad: Great place just not good when raining as the animals stay in
John Lee Ashworth: Love zoos, love animals, hate nothing but steep hills though
Clare: Very little animals to see.
Tammy 102: To check prices
Mark Woonton: Amazing zoo, so much to see, a great day out
So I am retrieving a json file from an API, and I want to grab the data from object 'place_id'.
I have been looking online and I have seen how to grab elements from json files, however I can't understand what I am doing wrong, as my code gives me an error:
Warning: Undefined array key "place_id" in C:\xampp\htdocs\cw\tester.php on line 15
This is my php code where I try to grab the data from 'place_id':
$place_json = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address=belfast+zoo&key=' . $place_id_key);
$json = json_decode($place_json, true);
echo $json['results']['place_id'];
this is my json file:
{
"results" : [
{
"address_components" : [
{
"long_name" : "Antrim Road",
"short_name" : "Antrim Rd",
"types" : [ "route" ]
},
{
"long_name" : "Belfast",
"short_name" : "Belfast",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Newtownabbey",
"short_name" : "Newtownabbey",
"types" : [ "postal_town" ]
},
{
"long_name" : "Belfast",
"short_name" : "Belfast",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Northern Ireland",
"short_name" : "Northern Ireland",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
},
{
"long_name" : "BT36 7PN",
"short_name" : "BT36 7PN",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Antrim Rd, Belfast, Newtownabbey BT36 7PN, UK",
"geometry" : {
"location" : {
"lat" : 54.6577821,
"lng" : -5.943665699999999
},
"location_type" : "GEOMETRIC_CENTER",
"viewport" : {
"northeast" : {
"lat" : 54.65913108029149,
"lng" : -5.942316719708497
},
"southwest" : {
"lat" : 54.6564331197085,
"lng" : -5.945014680291502
}
}
},
"place_id" : "ChIJ88iCe5inYUgRdeXmfCtuvu4",
"plus_code" : {
"compound_code" : "M354+4G Belfast, UK",
"global_code" : "9C6PM354+4G"
},
"types" : [ "establishment", "point_of_interest", "tourist_attraction", "zoo" ]
}
],
"status" : "OK"
}
Apologies as I know it is obvious, but I can't seem to understand why! thanks!
This should do the trick? Notice the [0] that was added. Since results is actually an array.
<?php
$place_json = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address=belfast+zoo&key=' . $place_id_key);
$json = json_decode($place_json, true);
echo $json['results'][0]['place_id'];
I'm using an API to retrieve a json file, that I then want to echo out onto my webpage.
I am able to echo the data individually, like so :
echo $json2['result']['reviews'][0]['author_name'];
However, as soon as I try to loop through the data with a foreach loop(), i get the error:
Warning: Array to string conversion in C:\xampp\htdocs\cw\tester.php on line 36
Array
Warning: Attempt to read property "result" on array in C:\xampp\htdocs\cw\tester.php on line 38
Warning: Attempt to read property "reviews" on null in C:\xampp\htdocs\cw\tester.php on line 38
Warning: foreach() argument must be of type array|object, null given in C:\xampp\htdocs\cw\tester.php on line 38
I'm sure its simple as json isn't too complicated, but I can't get it to work, as I don't understand what my problem is.
this is the for loop I tried to create:
foreach ($json2->result->reviews as $my_data){
foreach ($my_data -> author_name as $names){
echo $names . '<br>';
}
}
and this is the json file (I want to grab data from 'reviews'):
{
"html_attributions" : [],
"result" : {
"address_components" : [
{
"long_name" : "Antrim Road",
"short_name" : "Antrim Rd",
"types" : [ "route" ]
},
{
"long_name" : "Belfast",
"short_name" : "Belfast",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Newtownabbey",
"short_name" : "Newtownabbey",
"types" : [ "postal_town" ]
},
{
"long_name" : "Belfast",
"short_name" : "Belfast",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Northern Ireland",
"short_name" : "Northern Ireland",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
},
{
"long_name" : "BT36 7PN",
"short_name" : "BT36 7PN",
"types" : [ "postal_code" ]
}
],
"adr_address" : "\u003cspan class=\"street-address\"\u003eAntrim Road\u003c/span\u003e, \u003cspan class=\"extended-address\"\u003eBelfast\u003c/span\u003e, \u003cspan class=\"locality\"\u003eNewtownabbey\u003c/span\u003e \u003cspan class=\"postal-code\"\u003eBT36 7PN\u003c/span\u003e, \u003cspan class=\"country-name\"\u003eUK\u003c/span\u003e",
"business_status" : "OPERATIONAL",
"formatted_address" : "Antrim Rd, Belfast, Newtownabbey BT36 7PN, UK",
"formatted_phone_number" : "028 9077 6277",
"geometry" : {
"location" : {
"lat" : 54.6577821,
"lng" : -5.943665699999999
},
"viewport" : {
"northeast" : {
"lat" : 54.65913108029149,
"lng" : -5.942316719708497
},
"southwest" : {
"lat" : 54.6564331197085,
"lng" : -5.945014680291502
}
}
},
"icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/generic_business-71.png",
"international_phone_number" : "+44 28 9077 6277",
"name" : "Belfast Zoo",
"photos" : [
{
"height" : 1243,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/111863060002111236588\"\u003eBelfast Zoo\u003c/a\u003e"
],
"photo_reference" : "ATtYBwLPDngDqoLqrE-KZnFdp94V6uH2rOa-x4B2A7GVXOKD7raGPWiKtjNeDKEDpXrvbl9hPrjiE82RrHy-WFTyaIM51I7lE4f_gQavJlNtEcI8QCyNlgXdNbdubkFBTwOQpYQojvtcLavYu6RIHmiAQA8DnO3IQpX7pd3OIUC2drtfN85w",
"width" : 1776
},
{
"height" : 2988,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/100936618118839321951\"\u003eAmanda Lewis\u003c/a\u003e"
],
"photo_reference" : "ATtYBwI4nDTtt9wy2B18qwRhRrAd90Z24tQL5KpKDYw8UqHLbMXeazhIQzG0B72gNJ5qASZT3lVzw9IQ8789ovoqmfvhBU9biLUSjYWDFZRUz-UBIAvc-7ma69soGhze5ysjzg9K67e14axna-npfP0A64cc2v5pHO14hph0l8uBRovb4tnL",
"width" : 5312
},
{
"height" : 2988,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/116390912159770135890\"\u003eBeti Na\u003c/a\u003e"
],
"photo_reference" : "ATtYBwIBVFK2xPo7SoaT35yaxTvJo27782EJWXiLAlB7MuA3XHAbRd_DfJ7T6ZBZLVmpeyCO7GgyF06kkPtDak58HlGEup-IAyGXoCS6byofWqzqjjwvPx_FmN5tY2XfSU4Ocvs-UYyxnCA9T0q00egfJCL92XTMLHib5TDqQeAQY3r6pMKS",
"width" : 5312
},
{
"height" : 9248,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/106644795451552578979\"\u003eMatthew mcmullan\u003c/a\u003e"
],
"photo_reference" : "ATtYBwL1S6nVnplt3cRoJgsifwFdNo6fgcgd2GLpJqNuYvCdWBCMkvXNcFMiQ07OOzptgfsiYedU0xgUzG5D_VRXVPddqwgZQa0A4uBXoX43GFAU9rh3AB0u8MKY0M_1FB56GwVBEToT7rlShjLS5_KI1hOowB4ZdptMI_twgQwkzcEmIkg0",
"width" : 6936
},
{
"height" : 2988,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/116390912159770135890\"\u003eBeti Na\u003c/a\u003e"
],
"photo_reference" : "ATtYBwKFyQSrzrvX0X4tDu26wN10cgbo5w6JIlRQTWC4ttbAboV7ICKCuLWVazvoTdpBT1UpKGTsQHyVoZhC5f-1CBYtwZQCuTrOyGy8at0zsHW9ENLjq5q_zvB9LCHdgCUWMgWnBRzlYonNYCkPzRu-qORlJSX8X66-miqdbtXUFxMz-o3H",
"width" : 5312
},
{
"height" : 3823,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/116284097622909280304\"\u003eBrian Cunningham\u003c/a\u003e"
],
"photo_reference" : "ATtYBwJwf5rfaz8OqJ4AbQMgxKU3rKUf-4O6_Z6ME6vuZ5X6zgaSKdqvN_IxHuYqZIZWtPBlTFeYs4AzcH9oXWHQjrKdX5nGXelND-OoMe_Srp5pgGwmEr2f3TVQFktQAyv3NBA5l4JcBiKlb9wR_EueGaiEI6aM6VQobg6ab8CTXTGQclB7",
"width" : 5735
},
{
"height" : 4000,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/109005734355336105418\"\u003eWilliam Young\u003c/a\u003e"
],
"photo_reference" : "ATtYBwKlMZ9zSIi-AkC5M9nyMiOo3ly_-NtKqrFpz5A-_YFXyEc-Mn9APluh__SgUHPwgbNkZ27BjUYtK2Deb5MHax6yICyIC9RzZGOD4AY_F1pSPUXwwkMwuW66JMx_ZsgW-GpkJKo9m89Cua_mFBOfwrvxSpm66lwfpuQUsUz2fibwMKEQ",
"width" : 2723
},
{
"height" : 3024,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/100719597211687837516\"\u003eJonathan Kelly\u003c/a\u003e"
],
"photo_reference" : "ATtYBwJtrdU_MSyxj8Tzh-wt4fJeI9xnMBJSYus2rCCBRhVb1z64xhreHcSB3-QLRvoVS-XYSYLMhEyvtIBdsiNowQaBBHJEAUXcWtpsyvRCYjZU8a9gr9SCJpmo_Wj42CJTxR65cKlGC7i5dEDgssPUqmRZug8B5AtjJ3tuzGUqIBKcNdjz",
"width" : 4032
},
{
"height" : 3024,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/104554106193648036788\"\u003eAjay Jose\u003c/a\u003e"
],
"photo_reference" : "ATtYBwJzspaF2TTMtjQYmDJ1_IOJmjH6apJBqz9UJ8POUX1M8airOQja9pZQmrK0SONdFQUgTV-HXFI57sGHFmhetWxMilc5jPU_JcBQT6bhyX6Wqt21tbsV1GM9ySMXbYqMYwlSMBGrP9fiK32hbkxy7A5ecjX88QCYdvVUGqf2hbB8o2bM",
"width" : 4032
},
{
"height" : 3024,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/100719597211687837516\"\u003eJonathan Kelly\u003c/a\u003e"
],
"photo_reference" : "ATtYBwKqikSfsSeOGFFpSIx1g9O1iDMA3Xn_gf--hC2I9pJCOzDLFMkkLghF9Sr3j6kftlrCx8SSVhEPuZOZXxBOanl3qUh4UA5bvV7QTt3tD5Qje9Tnkd4319h-91dBZshFvd3y8rJb5WK_98bzc06DMIjCtJOPcQ3j1jV0Pr-DpGsPOH0-",
"width" : 4032
}
],
"place_id" : "ChIJ88iCe5inYUgRdeXmfCtuvu4",
"plus_code" : {
"compound_code" : "M354+4G Belfast, UK",
"global_code" : "9C6PM354+4G"
},
"rating" : 4.3,
"reference" : "ChIJ88iCe5inYUgRdeXmfCtuvu4",
"reviews" : [
{
"author_name" : "Karen Hoad",
"author_url" : "https://www.google.com/maps/contrib/103129952317107981705/reviews",
"language" : "en",
"profile_photo_url" : "https://lh6.googleusercontent.com/-OkGM8wAyHz8/AAAAAAAAAAI/AAAAAAAAAAA/AMZuucn55YdMQZ1BX6ods-S34cieMQODrg/s128-c0x00000000-cc-rp-mo-ba5/photo.jpg",
"rating" : 4,
"relative_time_description" : "6 months ago",
"text" : "Great place just not good when raining as the animals stay in",
"time" : 1599236033
},
{
"author_name" : "John Lee Ashworth",
"author_url" : "https://www.google.com/maps/contrib/116452096322094109908/reviews",
"language" : "en",
"profile_photo_url" : "https://lh3.googleusercontent.com/-_ZpK7cdK1NI/AAAAAAAAAAI/AAAAAAAAAAA/AMZuuclmcdFSQNilc5MfhJ3m8rRqzAXO_A/s128-c0x00000000-cc-rp-mo/photo.jpg",
"rating" : 3,
"relative_time_description" : "6 months ago",
"text" : "Love zoos, love animals, hate nothing but steep hills though",
"time" : 1598880406
},
{
"author_name" : "Clare",
"author_url" : "https://www.google.com/maps/contrib/116989491220973031798/reviews",
"language" : "en",
"profile_photo_url" : "https://lh3.googleusercontent.com/a-/AOh14GjK9VEovk8XRG41K9vVtu0SKRIHQdXp6Gb79opr_g=s128-c0x00000000-cc-rp-mo-ba2",
"rating" : 2,
"relative_time_description" : "5 months ago",
"text" : "Very little animals to see.",
"time" : 1600628749
},
{
"author_name" : "Tammy 102",
"author_url" : "https://www.google.com/maps/contrib/111672456708879530776/reviews",
"language" : "en",
"profile_photo_url" : "https://lh5.googleusercontent.com/-ngjoEXxeE8Q/AAAAAAAAAAI/AAAAAAAAAAA/AMZuucnTOkOTeq8ecWLEVbSs7UBnsUPmSQ/s128-c0x00000000-cc-rp-mo-ba3/photo.jpg",
"rating" : 5,
"relative_time_description" : "5 months ago",
"text" : "To check prices",
"time" : 1600610117
},
{
"author_name" : "Mark Woonton",
"author_url" : "https://www.google.com/maps/contrib/106800516570033737478/reviews",
"language" : "en",
"profile_photo_url" : "https://lh3.googleusercontent.com/a-/AOh14GgnYZybrP-MTC4U45agdIlXMIFDIuH1v6roRuJAWQ=s128-c0x00000000-cc-rp-mo",
"rating" : 5,
"relative_time_description" : "6 months ago",
"text" : "Amazing zoo, so much to see, a great day out",
"time" : 1599993064
}
],
"types" : [ "zoo", "tourist_attraction", "point_of_interest", "establishment" ],
"url" : "https://maps.google.com/?cid=17203308759660029301",
"user_ratings_total" : 2714,
"utc_offset" : 0,
"vicinity" : "Antrim Road, Belfast",
"website" : "http://www.belfastzoo.co.uk/"
},
"status" : "OK"
}
I want to get the Place detail using the place id from google map webservice api
I have called the api url using: https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJN1t_tDeuEmsRUsoyG83frY4&key=YOUR_API_KEY
I am getting response from google like this :
{
"html_attributions" : [],
"result" : {
"address_components" : [
{
"long_name" : "5",
"short_name" : "5",
"types" : [ "floor" ]
},
{
"long_name" : "48",
"short_name" : "48",
"types" : [ "street_number" ]
},
{
"long_name" : "Pirrama Road",
"short_name" : "Pirrama Rd",
"types" : [ "route" ]
},
{
"long_name" : "Pyrmont",
"short_name" : "Pyrmont",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Council of the City of Sydney",
"short_name" : "Sydney",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "New South Wales",
"short_name" : "NSW",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Australia",
"short_name" : "AU",
"types" : [ "country", "political" ]
},
{
"long_name" : "2009",
"short_name" : "2009",
"types" : [ "postal_code" ]
}
],
"adr_address" : "5, \u003cspan class=\"street-address\"\u003e48 Pirrama Rd\u003c/span\u003e, \u003cspan class=\"locality\"\u003ePyrmont\u003c/span\u003e \u003cspan class=\"region\"\u003eNSW\u003c/span\u003e \u003cspan class=\"postal-code\"\u003e2009\u003c/span\u003e, \u003cspan class=\"country-name\"\u003eAustralia\u003c/span\u003e",
"formatted_address" : "5, 48 Pirrama Rd, Pyrmont NSW 2009, Australia",
"formatted_phone_number" : "(02) 9374 4000",
"geometry" : {
"location" : {
"lat" : -33.866651,
"lng" : 151.195827
},
"viewport" : {
"northeast" : {
"lat" : -33.8653881697085,
"lng" : 151.1969739802915
},
"southwest" : {
"lat" : -33.86808613029149,
"lng" : 151.1942760197085
}
}
},
"icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "4f89212bf76dde31f092cfc14d7506555d85b5c7",
"international_phone_number" : "+61 2 9374 4000",
"name" : "Google",
"place_id" : "ChIJN1t_tDeuEmsRUsoyG83frY4",
"rating" : 4.5,
"reference" : "CmRSAAAAjiEr2_A4yI-DyqGcfsceTv-IBJXHB5-W3ckmGk9QAYk4USgeV8ihBcGBEK5Z1w4ajRZNVAfSbROiKbbuniq0c9rIq_xqkrf_3HpZzX-pFJuJY3cBtG68LSAHzWXB8UzwEhAx04rgN0_WieYLfVp4K0duGhTU58LFaqwcaex73Kcyy0ghYOQTkg",
"reviews" : [
{
"author_name" : "Robert Ardill",
"author_url" : "https://www.google.com/maps/contrib/106422854611155436041/reviews",
"language" : "en",
"profile_photo_url" : "https://lh3.googleusercontent.com/-T47KxWuAoJU/AAAAAAAAAAI/AAAAAAAAAZo/BDmyI12BZAs/s128-c0x00000000-cc-rp-mo-ba1/photo.jpg",
"rating" : 5,
"relative_time_description" : "a month ago",
"text" : "Awesome offices. Great facilities, location and views. Staff are great hosts",
"time" : 1491144016
}
],
"scope" : "GOOGLE",
"types" : [ "point_of_interest", "establishment" ],
"url" : "https://maps.google.com/?cid=10281119596374313554",
"utc_offset" : 600,
"vicinity" : "5, 48 Pirrama Road, Pyrmont",
"website" : "https://www.google.com.au/about/careers/locations/sydney/"
},
"status" : "OK"
}
So please help me to get the Country and State name from the result
Try this and you will get the state and country
$json = json_decode($result);
foreach ($json->results as $result) {
foreach($result->address_components as $addressPart) {
if ((in_array('locality', $addressPart->types)) && (in_array('political', $addressPart->types)))
$city = $addressPart->long_name;
else if ((in_array('administrative_area_level_1', $addressPart->types)) && (in_array('political', $addressPart->types)))
$state = $addressPart->long_name;
else if ((in_array('country', $addressPart->types)) && (in_array('political', $addressPart->types)))
$country = $addressPart->long_name;
}
}
I'm trying to parse a response from a Google API.
Im wondering if there is an easy/efficient way of going about parsing this array in php to get the data from the 'address_components' array.
{
"results" : [
{
"address_components" : [
{
"long_name" : "10",
"short_name" : "10",
"types" : [ "street_number" ]
},
{
"long_name" : "Downing Street",
"short_name" : "Downing St",
"types" : [ "route" ]
},
{
"long_name" : "Westminster",
"short_name" : "Westminster",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "London",
"short_name" : "London",
"types" : [ "postal_town" ]
},
{
"long_name" : "Greater London",
"short_name" : "Greater London",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "England",
"short_name" : "England",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
},
{
"long_name" : "SW1A 2AA",
"short_name" : "SW1A 2AA",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "10 Downing St, Westminster, London SW1A 2AA, UK",
"geometry" : {
"location" : {
"lat" : 51.5033635,
"lng" : -0.1276248
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 51.5047124802915,
"lng" : -0.126275819708498
},
"southwest" : {
"lat" : 51.5020145197085,
"lng" : -0.128973780291502
}
}
},
"place_id" : "ChIJRxzRQcUEdkgRGVaKyzmkgvg",
"types" : [ "establishment", "point_of_interest" ]
}
],
"status" : "OK"
}
My initial method was for run a loop through the array and creating a new array and setting the key to the "types", but I ran into some issues where there was more than one option for "types"
$data='{
"results" : [
{
"address_components" : [
{
"long_name" : "10",
"short_name" : "10",
"types" : [ "street_number" ]
},
{
"long_name" : "Downing Street",
"short_name" : "Downing St",
"types" : [ "route" ]
},
{
"long_name" : "Westminster",
"short_name" : "Westminster",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "London",
"short_name" : "London",
"types" : [ "postal_town" ]
},
{
"long_name" : "Greater London",
"short_name" : "Greater London",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "England",
"short_name" : "England",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
},
{
"long_name" : "SW1A 2AA",
"short_name" : "SW1A 2AA",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "10 Downing St, Westminster, London SW1A 2AA, UK",
"geometry" : {
"location" : {
"lat" : 51.5033635,
"lng" : -0.1276248
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 51.5047124802915,
"lng" : -0.126275819708498
},
"southwest" : {
"lat" : 51.5020145197085,
"lng" : -0.128973780291502
}
}
},
"place_id" : "ChIJRxzRQcUEdkgRGVaKyzmkgvg",
"types" : [ "establishment", "point_of_interest" ]
}
],
"status" : "OK"
}';
$json=json_decode( $data );
if( $json->status=='OK' ){
$data=array();/* store results */
$results=$json->results;
foreach( $results as $index => $obj ){
$add=$obj->address_components;
foreach( $add as $address ){
$data[ $obj->formatted_address ][ implode( '-', array_values( $address->types ) ) ]=$address->long_name;
}
}
echo '<pre>',print_r($data,1),'</pre>';
}
Will output:
Array
(
[10 Downing St, Westminster, London SW1A 2AA, UK] => Array
(
[street_number] => 10
[route] => Downing Street
[neighborhood-political] => Westminster
[postal_town] => London
[administrative_area_level_2-political] => Greater London
[administrative_area_level_1-political] => England
[country-political] => United Kingdom
[postal_code] => SW1A 2AA
)
)