Skip to content
Advertisement

Join two associative array based on key using PHP

I have two arrays here:

<?php
    $array1 = array(
        array('id' => 1, 'name' => 'John Doe', 'age' => 20),
        array('id' => 2, 'name' => 'Mae Doe', 'age' => 17),
        array('id' => 3, 'name' => 'Mark Smith', 'age' => 35),
        array('id' => 4, 'name' => 'Selena Smith', 'age' => 15),
        array('id' => 5, 'name' => 'Shane Doe', 'age' => 26),
        array('id' => 6, 'name' => 'Will Smith', 'age' => 45)
    )
    
    $array2 = array(
        array('id' => 1, 'address' => 'Singapore'),
        array('id' => 4, 'address' => 'Japan'),
        array('id' => 5, 'address' => 'Korea')
    )
?>

I want to join them, kind of left join in mysql so the result will be like this

$result = array(
    array('id' => 1,'name' => 'John Doe','age' => 20, 'address' => 'Singapore'),
    array('id' => 2,'name' => 'Mae Doe','age' => 17),
    array('id' => 3,'name' => 'Mark Smith','age' => 35),
    array('id' => 4,'name' => 'Selena Smith','age' => 15, 'address' => 'Japan'),
    array('id' => 5,'name' => 'Shane Doe','age' => 26, 'address' => 'Korea'),
    array('id' => 6,'name' => 'Will Smith','age' => 45)
)

The address will be merged to $array1 based on the id.

Is this possible using PHP? Please help. Thanks.

Advertisement

Answer

You can simplify this problem by using array_column to re-index $array2 by the id values; then it’s simply a question of checking whether data exists for the id value in $array1:

$array1 = array(
    array('id' => 1, 'name' => 'John Doe', 'age' => 20),
    array('id' => 2, 'name' => 'Mae Doe', 'age' => 17),
    array('id' => 3, 'name' => 'Mark Smith', 'age' => 35),
    array('id' => 4, 'name' => 'Selena Smith', 'age' => 15),
    array('id' => 5, 'name' => 'Shane Doe', 'age' => 26),
    array('id' => 6, 'name' => 'Will Smith', 'age' => 45)
);
    
$array2 = array(
    array('id' => 1, 'address' => 'Singapore', 'phone_number' => '123', 'store_name' => 'ABC'),
    array('id' => 4, 'address' => 'Japan', 'phone_number' => '456', 'store_name' => 'DEF'),
    array('id' => 5, 'address' => 'Korea', 'phone_number' => '789', 'store_name' => 'GHI')
);

$addresses = array_column($array2, null, 'id');
foreach ($array1 as &$arr) {
    if (isset($addresses[$arr['id']])) $arr = array_merge($arr, $addresses[$arr['id']]);
}
print_r($array1);

Output:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => John Doe
            [age] => 20
            [address] => Singapore
            [phone_number] => 123
            [store_name] => ABC
        )
    [1] => Array
        (
            [id] => 2
            [name] => Mae Doe
            [age] => 17
        )
    [2] => Array
        (
            [id] => 3
            [name] => Mark Smith
            [age] => 35
        )
    [3] => Array
        (
            [id] => 4
            [name] => Selena Smith
            [age] => 15
            [address] => Japan
            [phone_number] => 456
            [store_name] => DEF
        )
    [4] => Array
        (
            [id] => 5
            [name] => Shane Doe
            [age] => 26
            [address] => Korea
            [phone_number] => 789
            [store_name] => GHI
        )
    [5] => Array
        (
            [id] => 6
            [name] => Will Smith
            [age] => 45
        )
)

Demo on 3v4l.org

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement