Merge or combine two arrays - only if specified id exists

Viewed 139

I would like to combine two arrays, but I don't know which is the right method (array_merge, array_combine, array_unique) in my case.

First array:

Array
(
    [0] => Array
        (
            [id_person] => 194
            [firstname] => Jesper
            [lastname] => Hansen 
            [pos] => 29
            [position] => 1
            [starter] => 0
            [subs] => 0
            [goal] => 0
            [yellow_card] => 
            [red_card] => 
        )

    [1] => Array
        (
            [id_person] => 195
            [firstname] => Mikkel
            [lastname] => Andersen 
            [pos] => 1
            [position] => 1
            [starter] => 1
            [subs] => 0
            [goal] => 0
            [yellow_card] => 
            [red_card] => 
        )

    [2] => Array
        (
            [id_person] => 197
            [firstname] => Alexander
            [lastname] => Scholz 
            [pos] => 6
            [position] => 2
            [starter] => 1
            [subs] => 0
            [goal] => 0
            [yellow_card] => 
            [red_card] => 
        )

    [3] => Array
        (
            [id_person] => 198
            [firstname] => Erik
            [lastname] => Sviatchenko 
            [pos] => 10
            [position] => 2
            [starter] => 1
            [subs] => 0
            [goal] => 0
            [yellow_card] => 
            [red_card] => 
        )

    [4] => Array
        (
            [id_person] => 199
            [firstname] => Kian
            [lastname] => Hansen 
            [pos] => 14
            [position] => 2
            [starter] => 1
            [subs] => 0
            [goal] => 0
            [yellow_card] => 
            [red_card] => 
        )

    [5] => Array
        (
            [id_person] => 204
            [firstname] => Manjrekar
            [lastname] => James 
            [pos] => 30
            [position] => 2
            [starter] => 0
            [subs] => 0
            [goal] => 0
            [yellow_card] => 
            [red_card] => 
        )

[6] => Array
        (
            [id_person] => 236
            [firstname] => Sebastian
            [lastname] => Buch 
            [pos] => 14
            [position] => 14
            [starter] => 0
            [subs] => 0
            [goal] => 0
            [yellow_card] => 
            [red_card] => 
        )

[7] => Array
        (
            [id_person] => 209
            [firstname] => Joel
            [lastname] => Andersson 
            [pos] => 18
            [position] => 3
            [starter] => 1
            [subs] => 0
            [goal] => 0
            [yellow_card] => 
            [red_card] => 
        )
)

Second array:

Array
(
    [0] => Array
        (
            [id_person] => 195
            [position] => 1
            [first] => Mikkel
            [last] => Andersen 
            [id_club] => 2
            [type] => 2
            [captain] => 0
            [pos] => 1
            [subsmin_in] => 
            [subsmin_out] => 
        )

    [1] => Array
        (
            [id_person] => 236
            [position] => 14
            [first] => Sebastian
            [last] => Buch 
            [id_club] => 2
            [type] => 2
            [captain] => 0
            [pos] => 14
            [subsmin_in] => 
            [subsmin_out] => 
        )

    [2] => Array
        (
            [id_person] => 209
            [position] => 3
            [first] => Joel
            [last] => Andersson 
            [id_club] => 2
            [type] => 2
            [captain] => 0
            [pos] => 18
            [subsmin_in] => 
            [subsmin_out] => 
        )

)

I would like to copy from the first array the starter, subs, goal, yellow card, red card items to the second array (only if id_person from first array exist in the second array)

Desired result:

Array
(
    [0] => Array
        (
            [id_person] => 195
            [position] => 1
            [first] => Mikkel
            [last] => Andersen 
            [id_club] => 2
            [type] => 2
            [captain] => 0
            [pos] => 1
            [subsmin_in] => 
            [subsmin_out] => 
            [starter] => 1
            [subs] => 0
            [goal] => 0
            [yellow_card] => 
            [red_card] => 
        )

    [1] => Array
        (
            [id_person] => 236
            [position] => 14
            [first] => Sebastian
            [last] => Buch 
            [id_club] => 2
            [type] => 2
            [captain] => 0
            [pos] => 14
            [subsmin_in] => 
            [subsmin_out] => 
            [starter] => 0
            [subs] => 0
            [goal] => 0
            [yellow_card] => 
            [red_card] => 
        )

    [2] => Array
        (
            [id_person] => 209
            [position] => 3
            [first] => Joel
            [last] => Andersson 
            [id_club] => 2
            [type] => 2
            [captain] => 0
            [pos] => 18
            [subsmin_in] => 
            [subsmin_out] => 
            [starter] => 1
            [subs] => 0
            [goal] => 0
            [yellow_card] => 
            [red_card] => 
        )

)

I hope that someone could help for me. Many thanks.

2 Answers

If I understand your requirements properly then you can do it with the help of foreach(), array_search() and array_column() like below,

foreach($second_array as $k=>$v){
    $id_person = $v['id_person'];
    $key = array_search($id_person, array_column($first_array, 'id_person'));
    if($key){
        $second_array[$k]['starter'] = $first_array[$key]['starter'];
        $second_array[$k]['subs'] = $first_array[$key]['subs'];
        $second_array[$k]['yellow_card'] = $first_array[$key]['yellow_card'];
        $second_array[$k]['red_card'] = $first_array[$key]['red_card'];
    }
}
print_r($second_array); 

OR using array_merge() instead of 4 lines.

foreach($second_array as $k=>$v){
    $id_person = $v['id_person'];
    $key = array_search($id_person, array_column($first_array, 'id_person'));
    if($key){
        array_merge($second_array[$k],['starter'=>$first_array[$key]['starter'],'subs'=>$first_array[$key]['subs'],'yellow_card'=>$first_array[$key]['yellow_card'],'red_card'=>$first_array[$key]['red_card']]);
    }
}
print_r($second_array); 

WORKING DEMO: https://3v4l.org/FeanU

This is another way of tackling the issue:

/*
    Go through both arrays and put the person_id as key in each array
    (e.g. [0]['person_id'] = 197 TO [197]['person_id'] etc)
*/
foreach($first_array as $key=>$item) {
    $first_array[$item['id_person']] = $item;
    unset($first_array[$key]); //remove current item
}
foreach($second_array as $key=>$item) {
    $second_array[$item['id_person']] = $item;
    unset($second_array[$key]); //remove current item
}

/*
    Go through second array. If first array contains the same person_id
    the key for person_id exists, that's why we can use isset() here
*/
foreach($second_array as $key=>$item) {
    if (isset($first_array[$key])) {
        //Just copy the part that you intented from first array
        //to this $second_array
        $second_array[$key]['starter'] = $first_array[$key]['starter'];
        $second_array[$key]['subs'] = $first_array[$key]['subs'];
        $second_array[$key]['goal'] = $first_array[$key]['goal'];
        $second_array[$key]['yellow_card'] = $first_array[$key]['yellow_card'];
        $second_array[$key]['red_card'] = $first_array[$key]['red_card'];
    }
}

This will result in:

Array
(
    [195] => Array
        (
            [id_person] => 195
            [position] => 1
            [first] => Mikkel
            [last] => Andersen
            [id_club] => 2
            [type] => 2
            [captain] => 0
            [pos] => 1
            [subsmin_in] => 
            [subsmin_out] => 
            [starter] => 1
            [subs] => 0
            [goal] => 3
            [yellow_card] => 2
            [red_card] => 
        )

    [236] => Array
        (
            [id_person] => 236
            [position] => 14
            [first] => Sebastian
            [last] => Buch
            [id_club] => 2
            [type] => 2
            [captain] => 0
            [pos] => 14
            [subsmin_in] => 
            [subsmin_out] => 
            [starter] => 0
            [subs] => 0
            [goal] => 0
            [yellow_card] => 
            [red_card] => 
        )

    [209] => Array
        (
            [id_person] => 209
            [position] => 3
            [first] => Joel
            [last] => Andersson
            [id_club] => 2
            [type] => 2
            [captain] => 0
            [pos] => 18
            [subsmin_in] => 
            [subsmin_out] => 
            [starter] => 1
            [subs] => 0
            [goal] => 0
            [yellow_card] => 
            [red_card] => 
        )

)

If you want indexes starting at zero you can just do:

$second_array = array_values($second_array);

and you will have:

Array
(
    [0] => Array
        (
            [id_person] => 195
            [position] => 1
            [first] => Mikkel
            [last] => Andersen
            [id_club] => 2
            [type] => 2
            [captain] => 0
            [pos] => 1
            [subsmin_in] => 
            [subsmin_out] => 
            [starter] => 1
            [subs] => 0
            [goal] => 3
            [yellow_card] => 2
            [red_card] => 
        )

    [1] => Array
        (
            [id_person] => 236
            [position] => 14
            [first] => Sebastian
            [last] => Buch
            [id_club] => 2
            [type] => 2
            [captain] => 0
            [pos] => 14
            [subsmin_in] => 
            [subsmin_out] => 
            [starter] => 0
            [subs] => 0
            [goal] => 0
            [yellow_card] => 
            [red_card] => 
        )

    [2] => Array
        (
            [id_person] => 209
            [position] => 3
            [first] => Joel
            [last] => Andersson
            [id_club] => 2
            [type] => 2
            [captain] => 0
            [pos] => 18
            [subsmin_in] => 
            [subsmin_out] => 
            [starter] => 1
            [subs] => 0
            [goal] => 0
            [yellow_card] => 
            [red_card] => 
        )

)

NOTE! I have changed some values just for testing purposes.

Related