Merge two php arrays on base on specific key and value

Viewed 42

I have two arrays:

    Array
    (
[0] => Array
    (
        [semCategoryDocID] => 5
        [semCategoryID] => 8
        [docTypeID] => 1
        [semCategoryDocRequired] => 0
        [semCategoryDocAtom] => 0
        [docTypeName] => ID
    )

[1] => Array
    (
        [semCategoryDocID] => 8
        [semCategoryID] => 8
        [docTypeID] => 2
        [semCategoryDocRequired] => 1
        [semCategoryDocAtom] => 1
        [docTypeName] => Passport
    )

[2] => Array
    (
        [semCategoryDocID] => 9
        [semCategoryID] => 8
        [docTypeID] => 5
        [semCategoryDocRequired] => 1
        [semCategoryDocAtom] => 0
        [docTypeName] => Request
    )

[3] => Array
    (
        [semCategoryDocID] => 14
        [semCategoryID] => 8
        [docTypeID] => 8
        [semCategoryDocRequired] => 0
        [semCategoryDocAtom] => 1
        [docTypeName] => Other
    )

[4] => Array
    (
        [semCategoryDocID] => 15
        [semCategoryID] => 8
        [docTypeID] => 4
        [semCategoryDocRequired] => 0
        [semCategoryDocAtom] => 1
        [docTypeName] => Doc GDPR
    )

)


Array
(
    [0] => Array
        (
        [applicationDocID] => 54
        [applicationID] => 8530
        [semCategoryDocID] => 5
        [applicationDocName] => 8530_2014-09-09-1739319PAR1.pdf
        [applicationDocDate] => 2020-07-14 15:00:07
    )

[1] => Array
    (
        [applicationDocID] => 55
        [applicationID] => 8530
        [semCategoryDocID] => 9
        [applicationDocName] => 8530_2014-09-09-17465716PAR.pdf
        [applicationDocDate] => 2020-07-14 15:00:07
    )

)

I am trying to create new array on from those 2, which will include all elements from both arrays on base on the key semCategoryDocID

So the new array will have 5 elements, but 2 of them will have extra fields, which comes from array 2.

I tried the following code:

foreach($possible_documents_list as $key => $d){
    $possible_documents_list[$key]['applicationDocID'] = '';
    $possible_documents_list[$key]['applicationID'] = '';
    $possible_documents_list[$key]['applicationDocName'] = '';
    $possible_documents_list[$key]['applicationDocDate'] = '';                               
}            

foreach($uploaded_additional_documents as $key){
    if(in_array($key['semCategoryDocID'], array_column($possible_documents_list, 'semCategoryDocID'))) {
        $possible_documents_list['applicationDocID'] = '';
        $possible_documents_list['applicationID'] = '';
        $possible_documents_list['applicationDocName'] = '';
        $possible_documents_list['applicationDocDate'] = '';       
    } 
}

But without success, so any help will be deeply appreciated.

1 Answers

You might use 2 foreach loops and compare the values of semCategoryDocID.

When they match, use array_merge and add the value to a new $result array.

$result = [];
foreach ($array1 as $a1) {
    foreach ($array2 as $a2) {
        if ($a1["semCategoryDocID"] === $a2["semCategoryDocID"]) {
            $a1 = array_merge($a1, $a2);
        }
    }
    $result[] = $a1;
}

print_r($result);

Php demo

Related