我有来自数据库的“属性”。每个属性都有许多值。现在我想混合这些值来创建独特的组合。
输入示例:
$a = array(
35=>array('green','red','brown'),
36=>array('fox','house','dog')
);
输出 - 第二维的元素的所有可能组合。示例输出如下:
$output = array(
array(35=>'green',36=>'fox'),
array(35=>'green',36=>'house'),
array(35=>'green',36=>'dog'),
array(35=>'red',36=>'fox'),
array(35=>'red',36=>'house'),
array(35=>'red',36=>'dog'),
array(35=>'brown',36=>'fox'),
array(35=>'brown',36=>'house'),
array(35=>'brown',36=>'dog'),
);
我的功能没有再发生:
function myfunction($a){
$keys = array_keys($a);
$result = array();
if(count($keys)==0){
$result = array();
}
elseif(count($keys)==1){
$k = $keys[0];
foreach($a[$k] as $v){
$result[] = array($k=>$v);
}
}
elseif(count($keys)==2){
$k1 = $keys[0];
$k2 = $keys[1];
foreach($a[$k1] as $v1){
foreach($a[$k2] as $v2){
$result[] = array($k1=>$v1,$k2=>$v2);
}
}
}
elseif(count($keys)==3){
$k1 = $keys[0];
$k2 = $keys[1];
$k3 = $keys[2];
foreach($a[$k1] as $v1){
foreach($a[$k2] as $v2){
foreach($a[$k3] as $v3){
$result[] = array($k1=>$v1,$k2=>$v2,$k3=>$v3);
}
}
}
}
else{
throw new Exception('To much keys', 1);
}
return $result;
}
答案 0 :(得分:3)
这应该适合你:
那么这段代码做了什么?
1。有多少种组合?
首先问题是有多少组合,答案是你必须将每个数组的数量相乘。
所以(c =金额 1 ):
c array 1 * c array 2 * ... * c array n
具体针对您的示例:
c array 1 * c array 2 = 3 * 3 = 9
* 1 如果你想知道为什么我选择c
作为金额,因为php中的函数count()
2。获得所有组合
我们如何获得所有数组长度的所有组合?
非常简单,我们只是遍历所有组合(在开始时只是一个空组合([] == array()
)),我们已经拥有了下一个数组,直到我们得到所需的长度,在这种情况下最后一个数组的最后一次迭代。
以此为例:
Array with the elements (Empty array is '[]'):
[
[1, 2],
[3, 4]
]
//new combinations for the next iteration
|
array NAN*:
Combinations:
- [] | -> []
|
array 1 [1,2]: -------------
| |
Combinations: v v
- [] + 1 | -> [1]
- [] + 2 | -> [2]
|
array 2 [3,4]: -------------
| |
Combinations: v v
- [] + 3 | -> [3]
- [] + 4 | -> [4]
- [1] + 3 | -> [1,3]
- [1] + 4 | -> [1,4]
- [2] + 3 | -> [2,3]
- [2] + 4 | -> [2,4]
//^ All combinations here
* NAN:不是数字
正如您在上面的示例中所看到的,我们现在拥有了所有数组长度的所有组合。
但是为了只获得具有所需长度的组合,我们每次迭代都会覆盖结果数组,因此最后只有具有预期长度的组合在结果数组中。
代码:
<?php
$data = [
35 => ["green", "red", "brown"],
36 => ["fox", "house", "dog"]
];
$combinations = [[]];
$comKeys = array_keys($data);
for ($count = 0; $count < count($comKeys); $count++) {
$tmp = [];
foreach ($combinations as $v1) {
foreach ($data[$comKeys[$count]] as $v2)
$tmp[] = $v1 + [$comKeys[$count] => $v2];
}
$combinations = $tmp;
}
print_r($combinations);
?>
输出:
Array
(
[0] => Array
(
[35] => green
[36] => fox
)
[1] => Array
(
[35] => green
[36] => house
)
[2] => Array
(
[35] => green
[36] => dog
)
[3] => Array
(
[35] => red
[36] => fox
)
[4] => Array
(
[35] => red
[36] => house
)
[5] => Array
(
[35] => red
[36] => dog
)
[6] => Array
(
[35] => brown
[36] => fox
)
[7] => Array
(
[35] => brown
[36] => house
)
[8] => Array
(
[35] => brown
[36] => dog
)
)
答案 1 :(得分:0)
你可以使用:https://gist.github.com/jwage/11193216
或
使用下面的代码:
<?php
$attributes = [
[
'P', 'M', 'G', 'XG'
],
[
'Vermelho', 'Amarelo', 'Verde'
],
];
function array_mix($leftItems, $rightItems)
{
$results = [];
foreach ($leftItems as $leftItem) {
foreach ($rightItems as $key => $rightItem) {
$results[] = array_merge((array) $leftItem, (array) $rightItem);
}
}
return $results;
}
$y = $attributes[0];
foreach($attributes as $key => $attrs) {
if(isset($attributes[$key + 1]) && is_array($attributes[$key + 1])) {
$y = array_mix($y, $attributes[$key + 1]);
}
}
var_dump($y);