我需要从一组过滤器/参数中为所有可能的组合创建一个网址列表。
输入
$data = array(
array(
'vehicle=car',
'vehicle=bike',
'vehicle=plane',
),
array(
'fruit=apple',
'fruit=banana',
'fruit=strawberry'
),
array(
'music=pop',
'music=rock',
'music=jazz'
)
);
生成的项目必须按字母顺序排列参数 例如:
INCORRECT: ?vehicle=bike&fruit=apple&music=rock
CORRECT: ?fruit=apple&music=rock&vehicle=bike
输出
?vehicle=car
?vehicle=bike
?vehicle=plane
?fruit=apple&vehicle=car
?fruit=banana&vehicle=car
?fruit=strawberry&vehicle=car
?fruit=apple&vehicle=bike
?fruit=banana&vehicle=bike
?fruit=strawberry&vehicle=bike
?fruit=apple&vehicle=plane
?fruit=banana&vehicle=plane
?fruit=strawberry&vehicle=plane
?fruit=apple&music=pop&vehicle=car
?fruit=apple&music=rock&vehicle=car
?fruit=apple&music=jazz&vehicle=car
?fruit=banana&music=pop&vehicle=car
?fruit=banana&music=rock&vehicle=car
?fruit=banana&music=jazz&vehicle=car
?fruit=strawberry&music=pop&vehicle=car
?fruit=strawberry&music=rock&vehicle=car
?fruit=strawberry&music=jazz&vehicle=car
?fruit=apple&music=pop&vehicle=bike
?fruit=apple&music=rock&vehicle=bike
?fruit=apple&music=jazz&vehicle=bike
?fruit=banana&music=pop&vehicle=bike
?fruit=banana&music=rock&vehicle=bike
?fruit=banana&music=jazz&vehicle=bike
?fruit=strawberry&music=pop&vehicle=bike
?fruit=strawberry&music=rock&vehicle=bike
?fruit=strawberry&music=jazz&vehicle=bike
?fruit=apple&music=pop&vehicle=plane
?fruit=apple&music=rock&vehicle=plane
?fruit=apple&music=jazz&vehicle=plane
?fruit=banana&music=pop&vehicle=plane
?fruit=banana&music=rock&vehicle=plane
?fruit=banana&music=jazz&vehicle=plane
?fruit=strawberry&music=pop&vehicle=plane
?fruit=strawberry&music=rock&vehicle=plane
?fruit=strawberry&music=jazz&vehicle=plane
?music=pop&vehicle=car
?music=rock&vehicle=car
?music=jazz&vehicle=car
?music=pop&vehicle=bike
?music=rock&vehicle=bike
?music=jazz&vehicle=bike
?music=pop&vehicle=plane
?music=rock&vehicle=plane
?music=jazz&vehicle=plane
?fruit=apple
?fruit=banana
?fruit=strawberry
?fruit=apple&music=pop
?fruit=apple&music=rock
?fruit=apple&music=jazz
?fruit=banana&music=pop
?fruit=banana&music=rock
?fruit=banana&music=jazz
?fruit=strawberry&music=pop
?fruit=strawberry&music=rock
?fruit=strawberry&music=jazz
?music=pop
?music=rock
?music=jazz
有没有人可以帮我解决这个问题。我已经两天苦苦挣扎了,但我似乎无法找到正确的解决方案。 Stackoverflow上有很多(几乎)类似的问题,但它们似乎都没有解决/适合我的问题。
[解决] 这是基于Dusan Plavak答案的最终工作版本:
function createFilterCombinations($data, &$urls = array(), $index = 0, $query = false){
$keys = array_keys($data);
$_query = $query;
if ($index == count($data)) {
return;
}
for($i=0; $i < count($data[$keys[$index]]); $i++){
$query = $_query;
if($index == 0){
$query = "?" . $data[$keys[$index]][$i];
}else{
if($query != "?"){
$query .= "&" . $data[$keys[$index]][$i];
}else{
$query .= $data[$keys[$index]][$i];
}
}
$urls[] = $query;
createFilterCombinations($data, $urls, $index+1, $query);
}
if($index == 0){
$query = "?";
} else {
$query = $_query;
}
createFilterCombinations($data, $urls, $index+1, $query);
}
function prepareArray($array){
$newArray = array();
foreach ($array as $subArray) {
sort($subArray);
$newArray[substr($subArray[0], 0, strpos($subArray[0], '='))] = $subArray;
}
ksort($newArray);
return $newArray;
}
createFilterCombinations(prepareArray($data), $result);
var_dump($result);
答案 0 :(得分:2)
请看这个http://codepad.org/TZWf7Vxd
和链接将死的时间的代码:D
<?php
$data = array(
"vehicle" => array(
'vehicle=car',
'vehicle=bike',
'vehicle=plane',
),
"fruit" => array(
'fruit=apple',
'fruit=banana',
'fruit=strawberry'
),
"music" => array(
'music=pop',
'music=rock',
'music=jazz'
)
);
function hop($index, $query, $data){
$keys = array_keys($data);
if($index == count($data)){
return;
}
$queryBackup = $query;
for($i=0;$i<count($data[$keys[$index]]);$i++){
$query = $queryBackup;
if($index == 0){
$query = "?".$data[$keys[$index]][$i];
}else{
if($query != "?"){
$query .= "&".$data[$keys[$index]][$i];
}else{
$query .= $data[$keys[$index]][$i];
}
}
echo $query."\n";
hop($index+1, $query, $data);
}
if($index == 0){
$query = "?";
}else{
$query = $queryBackup;
}
hop($index+1, $query, $data);
}
ksort($data);
hop(0,"", $data);
?>
答案 1 :(得分:0)
这不是现成的解决方案,但您可以使用返回数组组合的函数。我希望这可以帮助你。
<?
$collect = false;
function combinations($arr, $temp_string, &$collect) {
if ($temp_string != "")
$collect[] = $temp_string;
for ($i = 0; $i < sizeof($arr); $i++) {
$arrcopy = $arr;
$elem = array_splice($arrcopy, $i, 1);
if (sizeof($arrcopy) > 0) {
combinations($arrcopy, $temp_string . " " . $elem[0], $collect);
} else {
$collect[] = $temp_string . " " . $elem[0];
}
}
return $collect;
}
var_dump(combinations(array('abc', 'cde', 'fgi'),'',$collect));
?>
请参阅WORKING CODE