我在PHP中有两个这样的数组:
$array1 = array(array("1", "3", "4"), array("1", "4"));
$array2 = array(array("5", "4", "3", "2"), array("5", "3"));
现在我想获得这两个多维数组的所有可能的交叉点。意味着我将获得4个阵列:
$array1[0]
& $array2[0]
$array1[1]
& $array2[0]
$array1[0]
& $array2[1]
$array1[1]
& $array2[1]
我可以使用array_intersect()
从一维数组中获取交集。但是,如何才能获得多个多维数组的所有可能的交叉点?
答案 0 :(得分:3)
这里我们创建一个包含所有数组组合的数组,以便稍后我们可以使用这些数组的交集。
首先,我们需要创建所有可能的数组组合。这是:
c array 1 * c array 2 * ... * c array n
<子>&#34; C&#34;只是意味着数组的count()
因此,在您的具体示例中,它将是:
c array 1 * c array 2 =&gt; 2 * 2 =&gt; 4种组合
现在我们需要获得所有这些组合并将它们放入数组中。为此,我们从一个空的$combinations
数组开始。然后我们循环遍历数组中的所有组合并将下一个数组合并到其中,直到我们得到所需的组合长度,在这种情况下是我们拥有的数组的数量。
以此为例:
Array with the elements (Empty array is '[]'):
[
[[1, 3, 4], [1, 4]], //array 1
[[5, 4, 3, 2], [5, 3]], //array 2
]
1* combination array 2* new array //↓new combinations
↓ ↓ //↓for the next iteration
│
array NAN*:
Combinations:
- [] │ -> []
│
array 1: ┌──────────────────────────────────┘
│
Combinations: v
- [] + [1, 3, 4] │ -> [[1, 3, 4]]
- [] + [1, 4] │ -> [[1, 4]]
│
array 2: ┌──────────────────────────────────┘
│
Combinations: v
- [[1, 3, 4]] + [5, 4, 3, 2] │ -> [[1, 3, 4], [5, 4, 3, 2]]
- [[1, 3, 4]] + [5, 3] │ -> [[1, 3, 4], [5, 3]]
- [[1, 4]] + [5, 4, 3, 2] │ -> [[1, 4], [5, 4, 3, 2]]
- [[1, 4]] + [5, 3] │ -> [[1, 4], [5, 3]]
//↑ All combinations here
* NAN:不是数字
正如您在上面的示例中所看到的,我们现在拥有所有数组长度的所有组合(4个组合,长度为2个元素)。
获取上述示例中显示的组合的代码是:
//The for loop makes sure we get the desired length of each combination //(The amount of arrays which we have. Here 2) for ($count = 0, $length = count($data); $count < $length; $count++) { $tmp = []; foreach ($combinations as $v1) { //1* combinations array foreach ($data[$count] as $v2) //2* new array $tmp[] = array_merge($v1, [$v2]); //Creating new combinations } $combinations = $tmp; //Assigning the new combinations for the next iteration }
在您的特定示例中生成此数组:
Array
(
[0] => Array //Combination 1
(
[0] => Array
(
[0] => 1
[1] => 3
[2] => 4
)
[1] => Array
(
[0] => 5
[1] => 4
[2] => 3
[3] => 2
)
)
[1] => Array //Combination 2
(
[0] => Array
(
[0] => 1
[1] => 3
[2] => 4
)
[1] => Array
(
[0] => 5
[1] => 3
)
)
[2] => Array //Combination 3
(
[0] => Array
(
[0] => 1
[1] => 4
)
[1] => Array
(
[0] => 5
[1] => 4
[2] => 3
[3] => 2
)
)
[3] => Array //Combination 4
(
[0] => Array
(
[0] => 1
[1] => 4
)
[1] => Array
(
[0] => 5
[1] => 3
)
)
)
&#13;
现在我们已经完成了所有组合,我们可以使用array_map()
来完成组合数组并获取每个组合的array_intersect()
。由于我们不知道我们想要交叉的阵列数量,我们只需使用call_user_func_array()
,例如
$intersections = array_map(function($v){
//intersection of each combination, which we created
return call_user_func_array("array_intersect", $v);
}, $combinations);
完整代码:
<?php
$array1 = [[1, 3, 4], [1, 4]];
$array2 = [[5, 4, 3, 2], [5, 3]];
function getIntersections($data = []) {
$combinations = [[]];
for ($count = 0, $length = count($data); $count < $length; $count++) {
$tmp = [];
foreach ($combinations as $v1) {
foreach ($data[$count] as $v2)
$tmp[] = array_merge($v1, [$v2]);
}
$combinations = $tmp;
}
$intersections = array_map(function($v){
return call_user_func_array("array_intersect", $v);
}, $combinations);
return $intersections;
}
$intersections = getIntersections([$array1, $array2]);
print_r($intersections);
?>
输出:
Array
(
[0] => Array //Intersection of: [1, 3, 4] && [5, 4, 3, 2]
(
[1] => 3
[2] => 4
)
[1] => Array //Intersection of: [1, 3, 4] && [5, 3]
(
[1] => 3
)
[2] => Array //Intersection of: [1, 4] && [5, 4, 3, 2]
(
[1] => 4
)
[3] => Array //Intersection of: [1, 4] && [5, 3]
(
)
)
答案 1 :(得分:1)
这foreach
解决了我的问题
$array1= array(array("1","3","4"),array("1","4"));
$array2= array(array("5","4","3","2"),array("5","3"));
$result = array();
echo "<pre>";
foreach($array1 as $array1)
{
foreach($array2 as $array3)
{
print_r($array3);
$result[] = array_intersect($array1,$array3);
}
}
print_r($result);
如果您有更好的解决方案,那么请改进它