N 数组的笛卡尔积
我有一个 PHP 数组,看起来像这个例子:
$array[0][0] = 'apples';
$array[0][1] = 'pears';
$array[0][2] = 'oranges';
$array[1][0] = 'steve';
$array[1][1] = 'bob';
我希望能够从中生成一个表,其中包含这些的每种可能的组合,但不重复任何组合(无论它们的位置如何),因此例如这将输出
Array 0 Array 1
apples steve
apples bob
pears steve
pears bob
But我希望它能够与尽可能多的不同数组一起使用。
I have a PHP array which looks like this example:
$array[0][0] = 'apples';
$array[0][1] = 'pears';
$array[0][2] = 'oranges';
$array[1][0] = 'steve';
$array[1][1] = 'bob';
And I would like to be able to produce from this a table with every possible combination of these, but without repeating any combinations (regardless of their position), so for example this would output
Array 0 Array 1
apples steve
apples bob
pears steve
pears bob
But I would like for this to be able to work with as many different arrays as possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我认为这是可行的 - 尽管在编写它之后我意识到它与其他人放置的非常相似,但它确实为您提供了所需格式的数组。抱歉变量命名不当。
This works I think - although after writing it I realised it's pretty similar to what others have put, but it does give you an array in the format requested. Sorry for the poor variable naming.
我将user187291的答案修改为:
所以它返回最重要的当您传递 0 个参数时,空数组(与没有组合的结果相同)。
只是注意到这一点,因为我正在使用它
I modified user187291's answer to be:
so it returns that all-important empty array (the same result as no combinations) when you pass 0 arguments.
Only noticed this because I'm using it like
我必须根据产品选项进行组合。该解决方案使用递归并适用于二维数组:
I had to make combinations from product options. This solution uses recursion and works with 2D array:
基于原生Python函数
itertools.product
的优雅实现Elegant implementation based on native Python function
itertools.product
这称为“笛卡尔积”,数组上的 php 手册页 http://php.net/ Manual/en/ref.array.php 显示了一些实现(在注释中)。
这是另一个:
this is called "cartesian product", php man page on arrays http://php.net/manual/en/ref.array.php shows some implementations (in comments).
and here's yet another one:
Syom 复制了 http://www.php.net/manual/en/ref .array.php#54979 但我将其改编为关联版本:
Syom copied http://www.php.net/manual/en/ref.array.php#54979 but I adapted it this to become an associative version:
您正在寻找数组的笛卡尔积,php 数组网站上有一个示例: http://php.net/manual/en/ref.array.php
You are looking for the cartesian product of the arrays, and there's an example on the php arrays site: http://php.net/manual/en/ref.array.php
我需要做同样的事情,我尝试了这里发布的以前的解决方案,但无法使它们工作。我从这个聪明的家伙那里得到了一个样本 http://www.php.net /manual/en/ref.array.php#54979。然而,他的样本没有管理不重复组合的概念。所以我包括了那部分。这是我的修改版本,希望它有所帮助:
结果会是这样的:
苹果 - 史蒂夫
苹果 - 鲍勃
梨子 - 史蒂夫
梨 - 鲍勃
橙子 - 史蒂夫
橙子 - bob
如果您的数据数组是这样的:
那么它将打印这样的内容:
惊人 - 好处 - 很棒
太棒了 - 优惠 - 太棒了
惊人 - 奖励 - 精彩
精彩 - 好处 - 惊人
太棒了 - 优惠 - 太棒了
精彩 - 奖励 - 惊人
I needed to do the same and I tried the previous solutions posted here but could not make them work. I got a sample from this clever guy http://www.php.net/manual/en/ref.array.php#54979. However, his sample did not managed the concept of no repeating combinations. So I included that part. Here is my modified version, hope it helps:
The result would be something like this:
apples - steve
apples - bob
pears - steve
pears - bob
oranges - steve
oranges - bob
If you the data array is something like this:
Then it will print something like this:
Amazing - benefit - Wonderful
Amazing - offer - Wonderful
Amazing - reward - Wonderful
Wonderful - benefit - Amazing
Wonderful - offer - Amazing
Wonderful - reward - Amazing