Is it possible to join 2 Arrays where the indexes are the same in PHP?

-3

Good, I have a problem with PHP Arrays. I'm doing an address lookup in 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, some addresses are not returning the zip code, due to some failure to register with Google itself. With this in my project check if the CEP came missing and if so I do a second search in the WebService viacep.com.br, and from there I get the zip code, however I need to reassemble this array that came from google by joining the field of zip code that came of ViaCEP to return in my main function. Knowing that the zip position in Google's Array when the address comes complete is:

['results'][0]['address_components'][6]['long_name']

I can set up an array with the same pattern as Google with only the zip code (given this returned from ViaCep's WebService), with the value in position 6 and its sub-keys 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 could not apply anything (in a practical way) to make this union! I tried using array_merge_recursive (); But it creates the second array as a different index! = /

I would like an exit joining this two as if the data had left one place!

Does anyone have any suggestions?

Thank you!

    
asked by anonymous 20.12.2017 / 07:38

3 answers

1

Assuming your google array is in a variable called $resultGoogle would look something like this:

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

So I understand you want to inject the zip into the address, you do not need to join the 2 array you can just modify the google array

    
20.12.2017 / 13:36
0

Good afternoon! ... With the example that Neuber Oliveira spent I started to think of reassembling the Array of google, based on this form of sum (+) between the parameters, hence I just managed to create a functional code, middle "Grotesque" but solves the problem, maybe in a foreach the code could get smaller or more direct to insertion! Basically what I did was recreating the keys, according to the original pattern and within the subkeys where it was array recreate the indexes and filled with the contents of the array $ resultGoogle in the specific key!

Basically the google Array pattern 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 Here is the code that 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 embarrassed that there is no simpler or more direct!

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

By the hour I thank you for the Light that you gave me Neuber! Open! = P

    
20.12.2017 / 21:37
0

I just got a more practical way! ... I do not know how I did not think of it! ... too hot head ends up letting the obvious go! The right way is to call the index ['address_components'] and get it to 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')
    )
);

By doing so he keeps all the data and includes only the new array at the end of the array of indexes ['address_components'], of course for this I must create the index array [6] and create its subkeys [ 'long_name'] and ['types']!

I now believe that there should be no simpler method than that! ... So that's the solution!

Thanks for the contributions! = D Hugs!

    
20.12.2017 / 22:54