PHP 中的组合、配置和排列

发布于 2024-08-09 21:07:19 字数 39 浏览 5 评论 0原文

在 PHP 中生成数组的所有组合、配置和排列的最有效方法是什么?

What is the most efficient way to generate all the combinations, dispositions and permutations of an array in PHP?

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

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

发布评论

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

评论(5

谈场末日恋爱 2024-08-16 21:07:19

以下是获取所有排列的代码:

http://php.net/manual/ en/function.shuffle.php#90615

通过获取幂集的代码,排列是最大长度的排列,幂集应该是所有组合。我不知道什么是倾向,所以如果你能解释一下,那会有所帮助。

Here is code to get all permutations:

http://php.net/manual/en/function.shuffle.php#90615

With the code to get the power set, permutations are those of maximal length, the power set should be all combinations. I have no idea what dispositions are, so if you can explain them, that would help.

﹉夏雨初晴づ 2024-08-16 21:07:19

您可以使用此类: http://pear.php.net/package/Math_Combinatorics

并使用它喜欢:

$combinatorics = new Math_Combinatorics;

$words_arr = array(
    'one'   => 'a',
    'two'   => 'b',
    'three' => 'c',
    'four'  => 'd',
    );

for ($i=count($words_arr)-1;$i>=1;$i--) {
    echo '<br><br>' . $i . ':<br>';
    $combinations_arr = $combinatorics->combinations($words_arr, $i);
    foreach ($combinations_arr as $combinations_arr_item) {
        echo implode(', ', $combinations_arr_item) . '<br>';
    }
}

You can use this class: http://pear.php.net/package/Math_Combinatorics

and use it like:

$combinatorics = new Math_Combinatorics;

$words_arr = array(
    'one'   => 'a',
    'two'   => 'b',
    'three' => 'c',
    'four'  => 'd',
    );

for ($i=count($words_arr)-1;$i>=1;$i--) {
    echo '<br><br>' . $i . ':<br>';
    $combinations_arr = $combinatorics->combinations($words_arr, $i);
    foreach ($combinations_arr as $combinations_arr_item) {
        echo implode(', ', $combinations_arr_item) . '<br>';
    }
}
难得心□动 2024-08-16 21:07:19

我想建议我的CombinationsGenerator解决方案,它生成数组项的组合。

仅限于所有组合均为完整长度,且不重复任何项目。但我相信实现不会太难。

class CombinationsGenerator
{
    public function generate(array $list): \Generator
    {
        if (count($list) > 2) {
            for ($i = 0; $i < count($list); $i++) {
                $listCopy = $list;

                $entry = array_splice($listCopy, $i, 1);
                foreach ($this->generate($listCopy) as $combination) {
                    yield array_merge($entry, $combination);
                }
            }
        } elseif (count($list) > 0) {
            yield $list;

            if (count($list) > 1) {
                yield array_reverse($list);
            }
        }
    }
}

$generator = new \CombinationsGenerator();

foreach ($generator->generate(['A', 'B', 'C', 'D']) as $combination) {
    var_dump($combination);
}

它采用 PHP7 风格,它使用 \Generator 因为我相信这样做有充分的理由。

I'd like to suggest my solution of a CombinationsGenerator, which generates combinations of array items.

It's limited to all combinations are of the full length, and not repeats any item. But I believe implementation would not be too hard.

class CombinationsGenerator
{
    public function generate(array $list): \Generator
    {
        if (count($list) > 2) {
            for ($i = 0; $i < count($list); $i++) {
                $listCopy = $list;

                $entry = array_splice($listCopy, $i, 1);
                foreach ($this->generate($listCopy) as $combination) {
                    yield array_merge($entry, $combination);
                }
            }
        } elseif (count($list) > 0) {
            yield $list;

            if (count($list) > 1) {
                yield array_reverse($list);
            }
        }
    }
}

$generator = new \CombinationsGenerator();

foreach ($generator->generate(['A', 'B', 'C', 'D']) as $combination) {
    var_dump($combination);
}

It's in PHP7 style, it uses a \Generator 'cause I believe there are good reasons for doing it.

年华零落成诗 2024-08-16 21:07:19
/* Combinations */
function nCr($n, $r)
{
  if ($r > $n)
  {
    return NaN;
  }
  if (($n - $r) < $r)
  {
    return nCr($n, ($n - $r));
  }
  $return = 1;
  for ($i = 0; $i < $r; $i++)
  {
    $return *= ($n - $i) / ($i +1);
  }
  return $return;
}

/* Permutations */
function nPr($n, $r)
{
  if ($r > $n)
  {
    return NaN;
  }
  if ($r)
  {
    return $n * (nPr($n -1, $r -1));
  }
  else
  {
    return 1;
  }
}
/* Combinations */
function nCr($n, $r)
{
  if ($r > $n)
  {
    return NaN;
  }
  if (($n - $r) < $r)
  {
    return nCr($n, ($n - $r));
  }
  $return = 1;
  for ($i = 0; $i < $r; $i++)
  {
    $return *= ($n - $i) / ($i +1);
  }
  return $return;
}

/* Permutations */
function nPr($n, $r)
{
  if ($r > $n)
  {
    return NaN;
  }
  if ($r)
  {
    return $n * (nPr($n -1, $r -1));
  }
  else
  {
    return 1;
  }
}
一向肩并 2024-08-16 21:07:19

我必须修改 @hejdav 的答案,使其包含部分组合,以便它完全提供所有结果。

我在互联网上搜索了这个解决方案,截至 2019 年 6 月,我相信这是唯一可公开访问的答案(在任何地方),真正列出了所有可能的、不可重复的可能性。

class CombinationsGenerator
{
    /**
     * Taken from https://stackoverflow.com/a/39447347/430062.
     * 
     * @param array $list
     * @return \Generator
     */
    public function generate(array $list): \Generator
    {
        // Generate even partial combinations.
        $list = array_values($list);
        $listCount = count($list);
        for ($a = 0; $a < $listCount; ++$a) {
            yield [$list[$a]];
        }

        if ($listCount > 2) {
            for ($i = 0; $i < count($list); $i++) {
                $listCopy = $list;

                $entry = array_splice($listCopy, $i, 1);
                foreach ($this->generate($listCopy) as $combination) {
                    yield array_merge($entry, $combination);
                }
            }
        } elseif (count($list) > 0) {
            yield $list;

            if (count($list) > 1) {
                yield array_reverse($list);
            }
        }
    }
}

I had to modify @hejdav's answer so that it includes partial combinations, so that it fully delivers all of the results.

I scoured the Internet for this solution and as of June 2019, I believe this is the only publicly accessible answer (anywhere) that truly lists all possible, non-duplicating possibilities.

class CombinationsGenerator
{
    /**
     * Taken from https://stackoverflow.com/a/39447347/430062.
     * 
     * @param array $list
     * @return \Generator
     */
    public function generate(array $list): \Generator
    {
        // Generate even partial combinations.
        $list = array_values($list);
        $listCount = count($list);
        for ($a = 0; $a < $listCount; ++$a) {
            yield [$list[$a]];
        }

        if ($listCount > 2) {
            for ($i = 0; $i < count($list); $i++) {
                $listCopy = $list;

                $entry = array_splice($listCopy, $i, 1);
                foreach ($this->generate($listCopy) as $combination) {
                    yield array_merge($entry, $combination);
                }
            }
        } elseif (count($list) > 0) {
            yield $list;

            if (count($list) > 1) {
                yield array_reverse($list);
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文