Array
(
[time] => Array
(
[0] => 1
[1] => 3
[2] => 5
)
[people] => Array
(
[0] => 2
[1] => 4
[2] => 3
)
[start] => Array
(
[0] => 1
[1] => 3
)
[other] => Array
(
[0] => 2
[1] => 3
)
……
)
把这样的数组变成,规则1:有序-由上到下的顺序,2:唯一 最后的结果是唯一不重复的
time:1;people:2;start:1;other:2;
time:1;people:4;start:1;other:2;
time:1;people:3;start:1;other:2;
time:1;people:2;start:3;other:2;
time:1;people:4;start:3;other:2;
time:1;people:4;start:3;other:2;
time:1;people:2;start:1;other:3;
time:1;people:4;start:1;other:3;
time:1;people:3;start:1;other:3;
time:1;people:2;start:3;other:3;
time:1;people:4;start:3;other:3;
time:1;people:4;start:3;other:3;
……
http://www.cnblogs.com/majiang/p/5990632.html
这个应该有36中变化,最笨的方法,无线枚举就可以了,4层for循环
for(int i=0;i<time.length;i++)
for(int j=0;j<people.length;j++)
for(int m=0;m<start.length;m++)
for(int n=0;n<other.length;n++)
System.out.println("time:"+i+";people:"+j+";start:"+m+";other:"+n+";");
你好,这里直接上代码了
public function combination($arr) {
$data = array();
foreach ($arr as $key => $val) {
$data = $this->_combination($data, $key, $val);
}
return $data;
}
public function _combination($data, $key, $arr)
{
$_data = array();
if (!$data) {
//第一次组合
foreach ($arr as $val) {
$_data[] = "{$key}:{$val};";
}
} else {
foreach ($data as $val) {
foreach ($arr as $v) {
$_data[] = $val . "{$key}:{$v};";
}
}
}
return $_data;
}
public function actionTestData() {
$arr = array(
'time' => array(1,3,5),
'people' => array(2,4,3),
'start' => array(1,3),
'other' => array(2,3),
);
$data = $this->combination($arr);
echo json_encode($data);
}
