寻找长度不足的数组的组合
我正在开发一个个人项目,并且一直致力于查找所有数组元素的组合,如下所示。当我有定义数量的数组时,我能够找到所有组合,但不幸的是我没有。
以下是记录的代码:
$C = array(
"I" => array(
0 => array("I_1_1"),
1 => array("I_1_2")
),
"L" => array(
0 => array("L_1_1"),
1 => array("L_2_1"),
),
"K" => array(
0 => array("K_1_1"),
1 => array("K_1_2"),
2 => array("K_2_1"),
3 => array("K_2_2"),
),
);
当前 $C 有 3 个元素,但这非常可变,因为您可以有 2 个以上 另外,$C 中的每个数组也是可变长度的,最大长度可以达到 144,但这非常罕见。
无论如何,我的目标是让 php 脚本输出这个:
I_1_1 L_1_1 K_1_1
K_1_2
K_2_1
K_2_2
L_2_1 K_1_1
K_1_2
K_2_1
K_2_2
I_1_2 L_1_1 K_1_1
K_1_2
K_2_1
K_2_2
L_2_1 K_1_1
K_1_2
K_2_1
K_2_2
我故意不重复输出中的前两列只是为了让您可以看到组合的“方向”,但最终输出将是一个包含所有可能组合的数组,例如:
$result = array(array("I_1_1","L_1_1","K_1_1"),array("I_1_1","L_1_1","K_1_2")....
等等..
我不知道我是否说清楚了或得到了你会更加困惑,但以防万一需要更多详细信息请告诉我。
预先感谢所有帮助我的人,我真的很感谢你们为帮助像我这样的人所付出的所有努力。
*编辑:*对我来说,更改数组结构比使用这个数组结构更容易生成排列会更好吗?
I am working on a personal project and i am stuck at finding the combinations of all array elements as shown below. I am able to find out all combinations when i have a defined number of arrays, but unfortunately i don't.
Here is the documented code:
$C = array(
"I" => array(
0 => array("I_1_1"),
1 => array("I_1_2")
),
"L" => array(
0 => array("L_1_1"),
1 => array("L_2_1"),
),
"K" => array(
0 => array("K_1_1"),
1 => array("K_1_2"),
2 => array("K_2_1"),
3 => array("K_2_2"),
),
);
Currently $C has 3 elements, but this is very variable, as in you can have 2+
Also, each array inside $C is of variable length too and can reach a max length of 144 but this is very rare..
Anyway, my goal is to get the php script to ouput this :
I_1_1 L_1_1 K_1_1
K_1_2
K_2_1
K_2_2
L_2_1 K_1_1
K_1_2
K_2_1
K_2_2
I_1_2 L_1_1 K_1_1
K_1_2
K_2_1
K_2_2
L_2_1 K_1_1
K_1_2
K_2_1
K_2_2
I made it on purpose not to repeat the first 2 columns in the output just so you can see the "direction" of the combination but eventually the output will be an array containing all possible combinations like such :
$result = array(array("I_1_1","L_1_1","K_1_1"),array("I_1_1","L_1_1","K_1_2")....
and so on..
I don't know if i made myself clear or got you even more confused but in case any more details are needed please let me know.
Thanks in advance to everyone who helps me out, i really appreciate all the effort you guys put in to help people like me.
*Edit: * Would it be better for me to change the array structure than to use this one to be able to generate the permutations easier?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果数组只能达到三层深度,你可以这样做:
如果它是可变深度,那么你将需要一些很棒的递归。
If the array can only go three deep, you can do:
If it's variable depth, then you'll need some awesome recursion.