需要一种从两个数组输出合并内容的方法


Need a way to output merged content from two arrays

我有两个非常大的数组,我想要识别它在第一个数组中找到重复项的位置,并将它们与另一个数组中的精确数组位置合并,并用逗号附加值。我简直想不出该怎么做。

//array with the multiple entries.
$applicationid = array('1','1','2','3','3','4','5','6','6','7','8','9','10','11','12','13','13','14','14','15','16','17','18','18');
$applicantid = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','11','22','25');

当它们并排放置时,很容易看到它们是这样的。

applicationid on left.  applicantid on right.
    1   1
    1   2
    2   3
    3   4
    3   5
    4   6
    5   7
    6   8
    6   9
    7   10
    8   11
    9   12
    10  13
    11  14
    12  15
    13  16
    13  17
    14  18
    14  19
    15  20
    16  21
    17  11
    18  22
    18  25

我希望这两个数组的最终结果是:

[1]['1,2']
[2]['3']
[3]['4,5']
[4]['6']
[5]['7']
[6]['8,9']
[7]['10']
[8]['11']
[9]['12']
[10]['13']
[11]['14']
[12]['15']
[13]['16,17']
[14]['18,19']
[15]['20']
[16]['21']
[17]['11']
[18]['22,25']

我相信这对其他人来说很容易,但我似乎无法理解它或理解所需的语法。

另外要注意的是,数组要比这个大得多(大约1400个条目)。

这很简单。索引匹配非常方便(虽然它说数据是以不太合适的格式生成的)。

试着像这样合并它们:

$applicationid = array('1','1','2','3','3','4','5','6','6','7','8','9','10','11','12','13','13','14','14','15','16','17','18','18');
$applicantid = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','11','22','25');
$applicantsByApplications = [];
$applicantsCount = count($applicationid);
for ($i = 0; $i < $applicantsCount; $i++)
{
    $applicant = $applicantid[$i];
    $application = $applicationid[$i];
    if (!isset($applicantsByApplications[$application]))
        $applicantsByApplications[$application] = [];
    $applicantsByApplications[$application][] = $applicant;
}

这可以使用标准函数,array_map等进行美化,但原则保持不变。