1

我在 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()。但是我怎样才能得到多个多维数组的所有可能的交集呢?

4

2 回答 2

3

在这里,我们创建了一个包含所有数组组合的数组,因此我们稍后可以获取这些数组的交集。

阵列组合

首先,我们需要创建所有可能的数组组合。这是:

c数组 1 * c数组 2 * ... * c数组 n

"c" 仅表示count()数组的

因此,在您的具体示例中,它将是:

c数组 1 * c数组 2 => 2 * 2 => 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 个元素)。

如上例所示,获取组合的代码是:

//for循环确保我们得到每个组合的所需长度
//(我们拥有的数组数量。这里是2)
for ($count = 0, $length = count($data); $count < $length; $count++) {  

    $tmp = [];

    foreach ($combinations as $v1) {   //1* 组合数组

        foreach ($data[$count] as $v2)   //2* 新数组
            $tmp[] = array_merge($v1, [$v2]); //创建新的组合

    }

    $组合 = $tmp; //为下一次迭代分配新的组合

}

在您的具体示例中生成此数组:

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
            )

    )

)

数组交集

现在我们有了所有组合,我们可以通过组合数组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]
        (
        )

)
于 2015-12-12T13:43:35.033 回答
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);

如果您有更好的解决方案,请改进它

于 2015-12-12T12:34:32.100 回答