在数组值中对大数组进行分组和过滤

时间:2013-12-31 11:23:37

标签: php arrays

我有一个像这样的数组

   Array
    (
        [0] => Array
            (
                [0] => 0
                [1] => 1
                [2] => 2
                [3] => 3
                [4] => 4
                [5] => 5
                [6] => 6
                [7] => 7
                [8] => 8
                [9] => 9
                [10] => 10
            )

        [1] => Array
            (
                [0] => 1
                [1] => 2
                [2] => 3
                [3] => 4
                [4] => 5
                [5] => 6
                [6] => 7
                [7] => 8
                [8] => 9
                [9] => 10
            )

        [2] => Array
            (
                [0] => 2
                [1] => 3
                [2] => 4
                [3] => 5
                [4] => 6
                [5] => 7
                [6] => 8
                [7] => 9
                [8] => 10
            )

        [3] => Array
            (
                [0] => 3
                [1] => 4
                [2] => 5
                [3] => 6
                [4] => 7
                [5] => 8
                [6] => 9
                [7] => 10
            )

        [4] => Array
            (
                [0] => 4
                [1] => 5
                [2] => 6
                [3] => 7
                [4] => 8
                [5] => 9
                [6] => 10
            )

        [5] => Array
            (
                [0] => 5
                [1] => 6
                [2] => 7
                [3] => 8
                [4] => 9
                [5] => 10
            )

        [50] => Array
            (
                [0] => 125
                [1] => 126
                [2] => 127
                [3] => 128
                [4] => 129
                [5] => 130
                [6] => 131
                [7] => 132
                [8] => 133
                [9] => 134
                [10] => 135
            )

        [51] => Array
            (
                [0] => 126
                [1] => 127
                [2] => 128
                [3] => 129
                [4] => 130
                [5] => 131
                [6] => 132
                [7] => 133
                [8] => 134
                [9] => 135
            )

        [52] => Array
            (
                [0] => 127
                [1] => 128
                [2] => 129
                [3] => 130
                [4] => 131
                [5] => 132
                [6] => 133
                [7] => 134
                [8] => 135
            )

        [53] => Array
            (
                [0] => 128
                [1] => 129
                [2] => 130
                [3] => 131
                [4] => 132
                [5] => 133
                [6] => 134
                [7] => 135
            )
    )

我上传的完整列表;

complete array element list

我必须将这些值分组为最大的父级。 例如,上面的代码将导致:

   Array
    (
        [0] => Array
            (
                [0] => 0
                [1] => 1
                [2] => 2
                [3] => 3
                [4] => 4
                [5] => 5
                [6] => 6
                [7] => 7
                [8] => 8
                [9] => 9
                [10] => 10
            )

        [50] => Array
            (
                [0] => 125
                [1] => 126
                [2] => 127
                [3] => 128
                [4] => 129
                [5] => 130
                [6] => 131
                [7] => 132
                [8] => 133
                [9] => 134
                [10] => 135
            )
    ) 

首先,我可以按值计数对它们进行排序,然后抓取最大值,但这是我的其他脚本的输出,可能会非常不同;

例如, 此输出有113个元素和两个组,但也许其他有8个组和80个元素 所以我需要一个解决方案来过滤所有重复的值,然后返回每个值组中最大的元素。

1 个答案:

答案 0 :(得分:0)

如果源数组始终预先订购为:

  1. 每个子数组的排序从较小的值到较大的值;
  2. 具有相同最大值的子数组按超集 - 子集顺序排序(即[0,1,2],[1,2],而不是[1,2],[0,1,2],而不是[0,1,3,4],[1,2,4]
  3. 然后,这可以通过foreach

    完成
    function reduce_array(array $source)
    {
        $picked_mark=array();
        $result=array();
        foreach ($source as $key=>$subarray)
        {
            if (in_array(end($subarray),$picked_mark)) continue;
            $result[$key]=$subarray;
            $picked_mark[]=end($subarray);
        }
        return $result;
    }
    

    Online demo