PHP 中的组合、配置和排列
在 PHP 中生成数组的所有组合、配置和排列的最有效方法是什么?
What is the most efficient way to generate all the combinations, dispositions and permutations of an array in PHP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
以下是获取所有排列的代码:
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.
您可以使用此类: http://pear.php.net/package/Math_Combinatorics
并使用它喜欢:
You can use this class: http://pear.php.net/package/Math_Combinatorics
and use it like:
我想建议我的CombinationsGenerator解决方案,它生成数组项的组合。
仅限于所有组合均为完整长度,且不重复任何项目。但我相信实现不会太难。
它采用 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.
It's in PHP7 style, it uses a
\Generator
'cause I believe there are good reasons for doing it.我必须修改 @hejdav 的答案,使其包含部分组合,以便它完全提供所有结果。
我在互联网上搜索了这个解决方案,截至 2019 年 6 月,我相信这是唯一可公开访问的答案(在任何地方),真正列出了所有可能的、不可重复的可能性。
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.