-1

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.

Codeblooded Saiyan
  • 1,457
  • 4
  • 28
  • 54
  • 1
    It is possible using PHP, but normally you would be expected to make some attempt before asking a question on SO. – Nigel Ren Aug 26 '20 at 10:11

2 Answers2

1

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

Nick
  • 138,499
  • 22
  • 57
  • 95
  • 1
    @JSN no worries. Note that this solution will be *significantly* faster than the other two as it only loops over each array once (it is `O(n+m)`), while they both loop array2 once for *every* element in array1 (they are `O(n*m)`). – Nick Aug 27 '20 at 03:12
  • yeah.. is it also possible on other fields, not only address? example my $array2 is `$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') );` – Codeblooded Saiyan Aug 27 '20 at 06:16
  • https://stackoverflow.com/a/64312585/2943403 – mickmackusa Jul 03 '22 at 10:33
  • @mickmackusa feel free to close as a dupe... it would be inappropriate for me to do so given that I have answered it. – Nick Jul 05 '22 at 03:46
1

Yes, it can. This is just another example how make it done:

<?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')
    );
    
    // The address will be merged to $array1 based on the id.
    array_walk($array1, function(&$item) use ($array2) {
        array_walk($array2, function($arr2) use (&$item) {
            if($item['id'] == $arr2['id']) {
                $item['address'] = $arr2['address'];
            }
        });
    });
    
    print_r($array1);
?>

Demo here

Idham Perdameian
  • 2,199
  • 24
  • 34