3

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

2 回答 2

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);
?>
于 2013-10-04T09:15:20.413 回答
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));
?>

工作代码

于 2013-10-04T09:39:46.800 回答