Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Working with 2 json arrays with connected data via ID

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[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?

>Solution :

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'];
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading