本文介绍了PHP清理置换的阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,基本上,我有一个数组:

 阵列('A','B','C');

现在我运行它通过一个数组置换函数,其结果是:

 阵列

    [0] =>排列
        (
            [0] => C
        )    [1] =>排列
        (
            [0] =>乙
        )    [2] =>排列
        (
            [0] =>乙
            [1] => C
        )    [3] =>排列
        (
            [0] => C
            [1] =>乙
        )    [4] =>排列
        (
            [0] =>一个
        )    [5] =>排列
        (
            [0] =>一个
            [1] => C
        )    [6] =>排列
        (
            [0] => C
            [1] =>一个
        )    [7] =>排列
        (
            [0] =>一个
            [1] =>乙
        )    [8] =>排列
        (
            [0] =>乙
            [1] =>一个
        )    [9] =>排列
        (
            [0] =>一个
            [1] =>乙
            [2] => C
        )    [10] =>排列
        (
            [0] =>一个
            [1] => C
            [2] =>乙
        )    [11] =>排列
        (
            [0] =>乙
            [1] =>一个
            [2] => C
        )    [12] =>排列
        (
            [0] =>乙
            [1] => C
            [2] =>一个
        )    [13] =>排列
        (
            [0] => C
            [1] =>一个
            [2] =>乙
        )    [14] =>排列
        (
            [0] => C
            [1] =>乙
            [2] =>一个
        ))

现在我的问题是,如何清洁数组了这样:

 阵列(C,B)
是相同的
阵列(B,C)

和它消除了第二个数组

我将如何做呢?

编辑...后根据您的回答一些研究,这是我想出了:

  array_walk($数组,'排序');
$阵列= array_unique($数组);排序($数组); // 不必要


解决方案

排序组成的数组:

 的foreach($数组AS&放大器; $ ARR)
{
   排序($ ARR);
}

所以{C,B}成为=> {B,C}结果
和{B,C}变为=> {B,C}

它们是相同的。

Hey all, basically, i have an array:

array('a', 'b', 'c');

Now i run it through an array permutation function and the result is:

Array
(
    [0] => Array
        (
            [0] => C
        )

    [1] => Array
        (
            [0] => B
        )

    [2] => Array
        (
            [0] => B
            [1] => C
        )

    [3] => Array
        (
            [0] => C
            [1] => B
        )

    [4] => Array
        (
            [0] => A
        )

    [5] => Array
        (
            [0] => A
            [1] => C
        )

    [6] => Array
        (
            [0] => C
            [1] => A
        )

    [7] => Array
        (
            [0] => A
            [1] => B
        )

    [8] => Array
        (
            [0] => B
            [1] => A
        )

    [9] => Array
        (
            [0] => A
            [1] => B
            [2] => C
        )

    [10] => Array
        (
            [0] => A
            [1] => C
            [2] => B
        )

    [11] => Array
        (
            [0] => B
            [1] => A
            [2] => C
        )

    [12] => Array
        (
            [0] => B
            [1] => C
            [2] => A
        )

    [13] => Array
        (
            [0] => C
            [1] => A
            [2] => B
        )

    [14] => Array
        (
            [0] => C
            [1] => B
            [2] => A
        )

)

Now my question is, how can i clean that array up so that:

array ( C, B )
is the same as
array ( B, C )

and it removes the second array

How would i do that?

EDIT... after some research based on your answers, this is what I came up with:

array_walk($array, 'sort');
$array = array_unique($array);

sort($array); // not necessary
解决方案

Just sort the constituent arrays:

foreach ($arrays AS &$arr)
{
   sort($arr);
}

So {"C", "B"} becomes => {"B", "C"}
and {"B", "C"} becomes => {"B", "C"}

which are identical.

这篇关于PHP清理置换的阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 22:49