我有两个非常大的数组,我想识别它在第一个数组上找到重复的位置,并将它们与另一个数组中的确切数组位置合并,并用逗号附加值。我根本无法理解如何去做。
//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个左右的条目)。
答案 0 :(得分:2)
这很简单。索引匹配非常方便(虽然它表示数据是以不太合适的格式生成的)。
尝试合并它们:
$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
等来美化,但原则保持不变。