如果数组的值存在于先前的键中,则删除该键

时间:2019-01-30 20:20:42

标签: php arrays sorting combinations

Array
(
    [0 0 0 4] => Array
        (
            [0] => 0 0
            [1] => 0 1
            [2] => 0 2
            [3] => 0 3
            [4] => 0 4
        )

    [0 1 0 5] => Array
        (
            [0] => 0 1
            [1] => 0 2
            [2] => 0 3
            [3] => 0 4
            [4] => 0 5
        )

    [0 1 4 1] => Array
        (
            [0] => 0 1
            [1] => 1 1
            [2] => 2 1
            [3] => 3 1
            [4] => 4 1
        )

    [0 2 0 5] => Array
        (
            [0] => 0 2
            [1] => 0 3
            [2] => 0 4
            [3] => 0 5
        )

    [0 2 0 6] => Array
        (
            [0] => 0 2
            [1] => 0 3
            [2] => 0 4
            [3] => 0 5
            [4] => 0 6
        )

    [0 3 1 4] => Array
        (
            [0] => 0 3
            [1] => 0 4
            [2] => 1 3
            [3] => 1 4
        )

    [0 3 3 3] => Array
        (
            [0] => 0 3
            [1] => 1 3
            [2] => 2 3
            [3] => 3 3
        )

    [0 3 4 3] => Array
        (
            [0] => 0 3
            [1] => 1 3
            [2] => 2 3
            [3] => 3 3
            [4] => 4 3
        )

    [0 5 1 6] => Array
        (
            [0] => 0 5
            [1] => 0 6
            [2] => 1 5
            [3] => 1 6
        )

    [0 5 4 5] => Array
        (
            [0] => 0 5
            [1] => 1 5
            [2] => 2 5
            [3] => 3 5
            [4] => 4 5
        )

    [0 6 3 6] => Array
        (
            [0] => 0 6
            [1] => 1 6
            [2] => 2 6
            [3] => 3 6
        )

    [0 6 4 6] => Array
        (
            [0] => 0 6
            [1] => 1 6
            [2] => 2 6
            [3] => 3 6
            [4] => 4 6
        )

    [1 0 2 1] => Array
        (
            [0] => 1 0
            [1] => 1 1
            [2] => 2 0
            [3] => 2 1
        )

    [1 1 4 1] => Array
        (
            [0] => 1 1
            [1] => 2 1
            [2] => 3 1
            [3] => 4 1
        )

    [1 1 5 1] => Array
        (
            [0] => 1 1
            [1] => 2 1
            [2] => 3 1
            [3] => 4 1
            [4] => 5 1
        )

    [1 2 5 2] => Array
        (
            [0] => 1 2
            [1] => 2 2
            [2] => 3 2
            [3] => 4 2
            [4] => 5 2
        )

    [1 4 2 5] => Array
        (
            [0] => 1 4
            [1] => 1 5
            [2] => 2 4
            [3] => 2 5
        )

    [1 5 5 5] => Array
        (
            [0] => 1 5
            [1] => 2 5
            [2] => 3 5
            [3] => 4 5
            [4] => 5 5
        )

    [2 1 2 5] => Array
        (
            [0] => 2 1
            [1] => 2 2
            [2] => 2 3
            [3] => 2 4
            [4] => 2 5
        )

    [2 2 2 5] => Array
        (
            [0] => 2 2
            [1] => 2 3
            [2] => 2 4
            [3] => 2 5
        )

    [2 2 2 6] => Array
        (
            [0] => 2 2
            [1] => 2 3
            [2] => 2 4
            [3] => 2 5
            [4] => 2 6
        )

    [2 2 3 3] => Array
        (
            [0] => 2 2
            [1] => 2 3
            [2] => 3 2
            [3] => 3 3
        )

    [2 2 5 2] => Array
        (
            [0] => 2 2
            [1] => 3 2
            [2] => 4 2
            [3] => 5 2
        )

    [2 5 5 5] => Array
        (
            [0] => 2 5
            [1] => 3 5
            [2] => 4 5
            [3] => 5 5
        )

    [3 0 3 3] => Array
        (
            [0] => 3 0
            [1] => 3 1
            [2] => 3 2
            [3] => 3 3
        )

    [3 0 3 4] => Array
        (
            [0] => 3 0
            [1] => 3 1
            [2] => 3 2
            [3] => 3 3
            [4] => 3 4
        )

    [3 1 4 2] => Array
        (
            [0] => 3 1
            [1] => 3 2
            [2] => 4 1
            [3] => 4 2
        )

    [3 4 4 5] => Array
        (
            [0] => 3 4
            [1] => 3 5
            [2] => 4 4
            [3] => 4 5
        )

    [4 5 5 6] => Array
        (
            [0] => 4 5
            [1] => 4 6
            [2] => 5 5
            [3] => 5 6
        )

)

例如,这是我的多重数组,每个键(0 1 0 5、0 1 4 1)都有其值。如果它们的值存在于先前的键数组中,那么我想将其组合成一组删除键。

例如:键“ 0 1 4 1”的值“ 0 1”存在于上一个键“ 0 1 0 5”中,在这种情况下,我们删除(取消设置)0 1 4 1。我将如何对所有元素执行此操作。并组合数组键的大小。希望你能理解

1 个答案:

答案 0 :(得分:0)

如果我正确理解了您要做什么,这是通过使用array_intersect()过滤集合数组的一种方法。仅当当前集合和上一个集合的交集为空时,过滤器回调才应返回true。

$result = array_filter($sets, function($set) {
    static $previous = [];
    $matches = array_intersect($set, $previous);
    $previous = $set;
    return !$matches;
});

答案的第一部分中的代码显示了如何删除每个具有与前面的单个数组相同的值的数组。如果您需要删除具有与先前数组的 any 相同的值的集合,则可以这样操作:

$result = array_filter($sets, function($set) {
    static $previous = [];
    foreach ($set as $value) {
        if (isset($previous[$value])) $matches = true;
        $previous[$value] = true;
    }
    return empty($matches);
});