-3
Good Guys, I have a problem related to the use of arrays in PHP. I’m doing an address search on google maps, and JSON returns me the following array:
Array
(
[results] => Array
(
[0] => Array
(
[address_components] => Array
(
[0] => Array
(
[long_name] => Rua Vicente Velasco
[short_name] => R. Vicente Velasco
[types] => Array
(
[0] => route
)
)
[1] => Array
(
[long_name] => Uep5-S.2
[short_name] => Uep5-S.2
[types] => Array
(
[0] => political
[1] => sublocality
[2] => sublocality_level_1
)
)
[2] => Array
(
[long_name] => Presidente Prudente
[short_name] => Pres. Prudente
[types] => Array
(
[0] => administrative_area_level_2
[1] => political
)
)
[3] => Array
(
[long_name] => São Paulo
[short_name] => SP
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[4] => Array
(
[long_name] => Brazil
[short_name] => BR
[types] => Array
(
[0] => country
[1] => political
)
)
)
[formatted_address] => R. Vicente Velasco - Uep5-S.2, Pres. Prudente - SP, Brazil
[geometry] => Array
(
[bounds] => Array
(
[northeast] => Array
(
[lat] => -22.0730082
[lng] => -51.3799775
)
[southwest] => Array
(
[lat] => -22.0748567
[lng] => -51.3807562
)
)
[location] => Array
(
[lat] => -22.0737691
[lng] => -51.3804065
)
[location_type] => GEOMETRIC_CENTER
[viewport] => Array
(
[northeast] => Array
(
[lat] => -22.072583469709
[lng] => -51.379017869709
)
[southwest] => Array
(
[lat] => -22.075281430292
[lng] => -51.381715830291
)
)
)
[partial_match] => 1
[place_id] => ChIJY3XgbV32k5QRQmh9Ne1joeo
[types] => Array
(
[0] => route
)
)
)
[status] => OK
)
However, for some addresses is not returning the zip code, due to some registration failure in Google itself. With that in my project I check if the zip code was missing and if so I do a second search in Webservice viacep.com.br, and from there I get the zip code, however I need to reassemble this array that came from google joining the ZIP field that came from Viacep to return in my main function. Knowing that the ZIP code position in the Google Array when the address comes complete is:
['results'][0]['address_components'][6]['long_name']
I can assemble an array with the same pattern as Google’s containing only the zip code (given this returned from Viacep’s Webservice), with the value at position 6 and its subkeys due:
Array
(
[results] => Array
(
[0] => Array
(
[address_components] => Array
(
[1] => Array
(
[] =>
)
[2] => Array
(
[] =>
)
[3] => Array
(
[] =>
)
[4] => Array
(
[] =>
)
[5] => Array
(
[] =>
)
[6] => Array
(
[long_name] => 19036-068
[types] => Array
(
[0] => postal_code
)
)
)
)
)
)
I looked at the PHP documentation but I couldn’t apply anything (practically) that makes this union! I tried using array_merge_recursive(); but it creates the second array as a different index! =/
I would like an exit uniting these two as if the data had come out of one place!
Does anyone have any suggestions?
Thank you!
Hello Neuber, Vlw for the help, but it hasn’t worked out yet! - you used the right word!... I want to inject this data into the Google Array... It turns out that this way that you put the method is returning an error that does not exist in $resultGoogle the Indice 6. And in fact when the google search returns without the zip code it does not load the Key [6]. would there be any way to include these keys in this Array? What you take is that there are several Arrays inside an Array, it’s a whole structure and I don’t know what else to do! I’m almost thinking of disassembling and reassembling the whole google array into a new foreach! =(
– Israel Felipe R. P.
@Israelfeliper. P.
estou quase pensando em desmontar e remontar o array do google todo num novo atraves de foreach!
And almost that, but instead of recreating the entire Voce structure, it will inject what it needs. I used as an example the Indice 6 because that’s what Voce posted, but you can simply create the missing Indice. I changed the answer a little to better illustrate.– Neuber Oliveira