为了清楚起见,假设我有一个像这样的数组
Array
(
[0] => 1234567
[1] => 1
[2] => 189818006457
[3] => 3
[4] => 10069992244808
[5] => 2
[6] => 100699922448
[7] => 5
[8] => 123454b
[9] => 6
[10] => 12345674b234
[11] => 2
[12] => 10
[13] => 3
[14] => 100699922
[15] => 1
[16] => 1006999
[17] => 30
[18] => 9
[19] => 36
[20] => 34
[21] => 45
[22] => 567
[23] => 3
[24] => 44
[25] => 3
[26] => 6788
[27] => 15
[28] => 189818006457371
[29] => 17
[30] => 123RT454b
[31] => 5
)
我想将它与下面的数组进行比较。因此,当下面的子数组1的值与上面的数组的值匹配时,系统将在组中的上述单维数组中打印匹配值之前的值。它还将检查下面的子数组2中的值是否与上面数组的值匹配,系统将在另一个组-2中打印上述单维数组中匹配值之前的值,依此类推。
Array
(
[0] => Array
(
[0] => 1
[1] => 3
[2] => 2
[3] => 5
[4] => 6
[5] => 2
[6] => 3
[7] => 1
[8] => 3
)
[2] => Array
(
[0] => 30
[1] => 36
[2] => 45
)
[1] => Array
(
[0] => 15
[1] => 17
)
)
抱歉我是php新手,但我必须做这个项目。感谢
答案 0 :(得分:0)
我建议先重新安排一下你拥有的数据:
为子数组数组创建一个映射,对于给定的值,它将返回该组的编号。
将1D输入数组转换为成对数组
然后你可以迭代对数组并在地图中执行查找。
以下是代码:
$input = [
'1234567', 1, '189818006457', 3, '10069992244808', 2,
'100699922448', 5, '123454b', 6, '12345674b234', 2,
'10', 3, '100699922', 1, '1006999', 30, '9', 36,
'34', 45, '567', 3, '44', 3, '6788', 15,
'189818006457371', 17, '123RT454b', 5
];
$groups = [
[1, 3, 2, 5, 6, 2, 3, 1, 3],
[30, 36, 45],
[15, 17]
];
// First create an associative array that maps a given value to a group number
$map = [];
foreach ($groups as $group => $subarray) {
$map = $map + array_combine($subarray, array_fill(0, count($subarray), $group));
}
// Now iterate pairs of the input data
foreach (array_chunk($input, 2) as list($v, $k)) {
$result[$map[$k]][] = $v ;
}
$result
变量将有三个子数组,每组一个:
[
[
'1234567', '189818006457', '10069992244808',
'100699922448', '123454b', '12345674b234',
'10', '100699922', '567', '44', '123RT454b'
], [
'1006999', '9', '34'
], [
'6788', '189818006457371'
]
]