Is it possible to join 2 arrays where the indexes are equal in PHP?

Asked

Viewed 619 times

-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!

3 answers

1

Assuming your google array is in a variable call $resultGoogle it would be something like that:

$itemAlvo = $resultGoogle['results'][0]['address_components'];
$itemAlvo[] = array(
    'long_name'=>$cep_do_viacep; //aqui vem o cep queobteve do ViaCEP
    'types'=> array('postal_code');
);

From what I understood Voce kind of wants to inject the cep in the address, do not need to join the 2 array Voce can only modify the google array

  • 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! =(

  • 1

    @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.

0

Good Afternoon!... Guy with the example that Neuber Oliveira went through I started thinking about reassembling the Array of google, based on this form of sum (+) between the parameters, then I just managed to create a functional code, it was kind of "Grotesque", but solves the problem, Maybe in a foreach the code could be smaller or more direct insertion! Basically what I did was recreate the keys, according to the original’s pattern and inside the subkeys where it was an array I recreated the indexes and filled with the contents of the $resultGoogle array in the specific key!

Basically the default google array is:

['results']
    [0]
        ['address_components']
            [1],[2],[3],[4],[5],[6]

        ['formatted_address']
        ['geometry']
            ['bounds'], ['location'], ['location_type'], ['viewport']

        ['partial_match']
        ['place_id']
        ['types']
['status']

Just to add the Key [6] and its values Follow the code I generated - based on the Solution presented by Neuber Oliveira:

$itemModificar = array(
    'results' => array(
                    0 => array(
                        'address_components' => $resultGoogle['results'][0]['address_components'] + 
                        array(6 => array('long_name' => $nBusca[0]['cep'],'types' => array(0 =>'postal_code'))
                        ),
                        'formatted_address' => $resultGoogle['results'][0]['formatted_address'],
                        'geometry' => array($resultGoogle['results'][0]['geometry']),
                        'partial_match' => $resultGoogle['results'][0]['partial_match'],
                        'place_id' => $resultGoogle['results'][0]['place_id'],
                         'types' => $resultGoogle['results'][0]['types'],),
    ),
    'status' => $resultGoogle['status']
);

For now, this solution solves the problem, but I’m still stuck with no simpler or more direct form!

If someone knows a different code within good practice, it would be interesting to use!

For now I thank the Light you gave me Neuber! abrss! = P

0

I just got a more practical way! I don’t know how I didn’t think of it!... too hot-headed to miss the obvious! The most certain output is to call the index ['address_components'] and make it receive the same components plus the new data!

Follow the most direct code:

$resultGoogle['results'][0]['address_components'] = 
$resultGoogle['results'][0]['address_components'] +
array(
    6 => array('long_name' => $nBusca[0]['cep'], 
    'types' => array(0 => 'postal_code')
    )
);

In doing so, they keep all the data and include only the new array at the end of the list of ['address_components' index arrays], of course, for this I must create the array of Dice [6] and create its subkeys ['long_name'] and ['types']!

I believe now that there should not be another simpler method than this!... Anyway it is there the Solution!

I appreciate the contributions! = D Hugs!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.