PHP 清理置换数组

发布于 2024-08-13 13:43:38 字数 1836 浏览 6 评论 0原文

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

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

现在我通过数组排列函数运行它,结果是:

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
        )

)

现在我的问题是,我如何清理该数组,以便:

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

并且它删除了第二个数组

我该怎么做?

编辑...根据您的答案进行一些研究后,这就是我得出的结论:

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

sort($array); // not necessary

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

贪恋 2024-08-20 13:43:38

只需 排序 组成数组:

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

所以 {"C", "B"}变成=> {“B”,“C”}
并且 {"B", "C"} 变为 => {“B”,“C”}

相同。

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.

半暖夏伤 2024-08-20 13:43:38
array_multisort($array);
array_unique($array);
array_multisort($array);
array_unique($array);
谜泪 2024-08-20 13:43:38

您还可以使用 pear 包 Math_Combinatorics

require_once 'Combinatorics.php';
$combinatorics = new Math_Combinatorics;
$a = array('a', 'b', 'c');

// creating and storing the combinations
for($combinations = array(), $n=1; $n<=count($a); $n++) {
  $combinations = array_merge($combinations, $combinatorics->combinations($a, $n));
}

// test output
foreach($combinations as $c) {
  echo join(', ', $c), "\n";
}

印刷

a
b
c
a, b
a, c
b, c
a, b, c

You can also use the pear package Math_Combinatorics.

require_once 'Combinatorics.php';
$combinatorics = new Math_Combinatorics;
$a = array('a', 'b', 'c');

// creating and storing the combinations
for($combinations = array(), $n=1; $n<=count($a); $n++) {
  $combinations = array_merge($combinations, $combinatorics->combinations($a, $n));
}

// test output
foreach($combinations as $c) {
  echo join(', ', $c), "\n";
}

prints

a
b
c
a, b
a, c
b, c
a, b, c
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文