Home > Enterprise >  Working with 2 json arrays with connected data via ID
Working with 2 json arrays with connected data via ID

Time:03-15

Been trying to work with two arrays and extract information from the arrays. I can do this with the foreach but I have linked file references (an ID) which targets data in another array.

Array 1 -

Array
(
    [type] => land
    [id] => 0b1b522e-0cd2-4880-b3ac-a5d86bc2e837       
    [relationships] => Array
        (
            [address] => Array
                (
                    [data] => Array
                        (
                            [type] => address
                            [id] => bb89e6c3-9114-4192-a0a7-82c08402d36e
                        )

                )
            [details] => Array
                (
                    [data] => Array
                        (
                            [type] => details
                            [id] => f8f0e489-bb86-4857-a815-ab6338d90f26
                        )

                )
            [lettingsListing] => Array
                (
                    [data] => 
                )

            [primaryImage] => Array
                (
                    [data] => Array
                        (
                            [type] => media
                            [id] => 58574088-01a2-45da-a66e-cd3ce1741377
                        )
                )
            [images] => Array
                (
                    [data] => Array
                        (
                            [0] => Array
                                (
                                    [type] => media
                                    [id] => 58574088-01a2-45da-a66e-cd3ce1741377
                                )
                            [1] => Array
                                (
                                    [type] => media
                                    [id] => 32d9d605-d55d-48b7-aa62-5e22f762f165
                                )
                            [2] => Array
                                (
                                    [type] => media
                                    [id] => c879656a-a34f-4c93-b6c7-49d3c7b11804
                                )
                            [3] => Array
                                (
                                    [type] => media
                                    [id] => 434f8d79-df19-474d-a275-6c9a5fb0985b
                                )
                            [4] => Array
                                (
                                    [type] => media
                                    [id] => cefccee0-cd30-4c69-9f6f-2bd76116a619
                                )
                            [5] => Array
                                (
                                    [type] => media
                                    [id] => c0377f6c-4279-4176-a5bd-0190b7fd97a8
                                )
                            [6] => Array
                                (
                                    [type] => media
                                    [id] => 6869c65a-bb55-4b9f-8dc1-71ffa5eb84dc
                                )
                            [7] => Array
                                (
                                    [type] => media
                                    [id] => c9b13725-059a-4c96-a8fd-77c9fc9fe05f
                                )
                            [8] => Array
                                (
                                    [type] => media
                                    [id] => edbdddb2-afc7-43ab-9a79-94e0e8e63597
                                )
                            [9] => Array
                                (
                                    [type] => media
                                    [id] => 320de2d7-6ecd-4ec7-9250-f72e228be8a7
                                )
                            [10] => Array
                                (
                                    [type] => media
                                    [id] => 63daaccf-d8d2-4b55-aece-5a01e379fba0
                                )
                            [11] => Array
                                (
                                    [type] => media
                                    [id] => 5d2c253a-03f5-4ea0-b06d-dd17c80d5f3b
                                )
                        )
                )
        )
)

The images in the array are like:

[0] => Array
            (
                [type] => media
                [id] => 58574088-01a2-45da-a66e-cd3ce1741377
            )

The ID then corresponds to another array (array 2)

Array
(
    [type] => media
    [id] => 58574088-01a2-45da-a66e-cd3ce1741377
    [attributes] => Array
        (
            [name] => p048205_01
            [order] => 1
            [is_featured] => 
            [feature_index] => 
            [title] => 
            [is_image] => 1
            [url] => ***urlhere***
                )
        )
)

So my question is how to get the associated data ([url]) with each ID from the other array. I thought about array_merge but that did not help. My only other thinking is to do a foreach inside the current loop but I have heard it best to not to that?

CodePudding user response:

Create a new array to hold the image urls from what you called array2, but which has the id as the key, so you can jump straight to the URL from your other foreach loop.

$img_urls = []
foreach( $array2 as $a ) {
    $img_urls[$a['id']] = $a;
}

Or if you only want the url in this new array

$img_urls = []
foreach( $array2 as $a ) {
    $img_urls[$a['id']] = $a['attributes']['url'];
}

CodePudding user response:

As RiggsFolly said, you need to manipulate the second array with images like this:

$new_arr_images = [];

foreach ($arr_images as $value) {
   $new_arr_images[$value['id']] = $value['attributes']['url'];
}

// then loop through first array and populate it with image urls based on the ID
  • Related