1、需求
- 将下方数组
$arr
格式化为[[A,B,C,D,E,F], [X,Y,Z]]
2、编码
$arr = [
['A', 'B'],
['A', 'C'],
['A', 'D'],
['C', 'E'],
['D', 'F'],
['X', 'Y'],
['X', 'Z']
];
$result = array();
foreach ($arr as $subArr) {
$merged = false;
foreach ($result as &$r) {
if (array_intersect($r, $subArr)) {
$r = array_unique(array_merge($r, $subArr));
$merged = true;
break;
}
}
if (!$merged) {
$result[] = $subArr;
}
}
print_r($result);
3、输出结果
[
0 => [
0 => "A"
1 => "B"
2 => "C"
3 => "D"
4 => "E"
6 => "F"
]
1 => [
0 => "X"
1 => "Y"
3 => "Z"
]
]
标签:arr,交集,二维,result,数组,array,php,merged,subArr
From: https://www.cnblogs.com/pine007/p/17436727.html