我需要为一组过滤器/参数中的所有可能组合创建一个 url 列表。
输入
$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);