我有两个数组:
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
)
)
我正在尝试从这2个数组中创建新的数组,它将包括两个数组中的所有元素基于关键字CategoryDocID
因此,新数组将有5个元素,但其中2个元素将有来自数组2的额外字段。
我尝试了以下代码:
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'] = '';
}
}
但如果没有成功,任何帮助都将不胜感激。
您可以使用2个foreach循环来比较semCategoryDocID
的值。
当它们匹配时,使用array_merge并将值添加到新的$result
数组中。
$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演示