字符串数组的排列

Tho*_*ard 5 php arrays permutation

我根本无法解决如何解决这个问题,经过彻底搜索谷歌没有任何结果,我转向你希望有一个解决方案.

鉴于下面的示例数组:

array(
    'Type' => array(
        'Toppe',
        'Bukser_og_Jeans'
    ),
    'Size' => array(
        'Extra_small',
        'Small'
    ),
    'Colour' => array(
        'Rod'
    )
)
Run Code Online (Sandbox Code Playgroud)

(注意:这仅仅是一个示例;实际的现实生活情况可能每组的组/和/或元素更少/更多)

我将如何得出以下结果?

Toppe,Extra_small,Rod
Toppe,Small,Rod
Bukser_og_Jeans,Extra_small,Rod
Bukser_og_Jeans,Small,Rod
Run Code Online (Sandbox Code Playgroud)

这是一个产品搜索,API只允许每个查询的每个类型,大小和颜色组的'细化'值,但我的分配需要查询和聚合多个API查询的结果.

,我需要某种形式的递归函数来做到这一点,但我一直无法甚至产生接近我的预期结果的任何代码.

我在谷歌上找到的只是关于字母甚至字符串的翻译,但人们需要的地方有"红色,蓝色,绿色","蓝色,红色,绿色","绿色,红色,蓝色"等. ,显然,不是我想要的.

我希望这里的某个人能够理解我想做的事情,并且知道如何去做.

编辑:由@ikegami发布的解决方案,转换为PHP:

$iter = 0;
while (1) {
    $num = $iter++;
    $pick = array();

    foreach ($refinements as $refineGroup => $groupValues) {
        $r = $num % count($groupValues);
        $num = ($num - $r) / count($groupValues);
        $pick[] = $groupValues[$r];
    }

    if ($num > 0) {
        break;
    }

    print join(', ', $pick)."\n";
}
Run Code Online (Sandbox Code Playgroud)

ike*_*ami 10

如果我们有三组十个项目,我们可以使用一个从0到999的计数器,并将数字分成数字.

例如,

456 
   % 10 = 6  --------------------------  Item 6 (7th item) in the first group
   / 10 = 45
             % 10 = 5  ----------------  Item 5 (6th item) in the second group
             / 10 = 4
                       % 10 = 4  ------  Item 4 (5th item) in the third group
                       / 10 = 0
Run Code Online (Sandbox Code Playgroud)

此算法将数字转换为基数10.如果我们想要转换为八进制,我们将使用8而不是10. 10(或8)被使用,因为每个位置具有相同数量的符号,但此算法也有效如果符号的数量因位置而异.

2
   % 2 = 0  ------------------------  Item 0 (1st item) in the first group: Toppe
   / 2 = 1
     ^      % 2 = 1  ---------------  Item 1 (2nd item) in the second group: Small
     |      / 2 = 0
     |        ^      % 1 = 0  ------  Item 0 (1st item) in the third group: Rod
     |        |      / 1 = 0
     |        |        ^
     |        |        |
     |        |        +------------  Number of items in third group
     |        +---------------------  Number of items in second group
     +------------------------------  Number of items in first group
Run Code Online (Sandbox Code Playgroud)

这给了我们:

0 = ( 0 * 1 + 0 ) * 2 + 0 = Toppe, Extra_small, Rod
1 = ( 0 * 1 + 0 ) * 2 + 1 = Bukser_og_Jeans, Extra_small, Rod
2 = ( 0 * 1 + 1 ) * 2 + 0 = Toppe, Small, Rod
3 = ( 0 * 1 + 1 ) * 2 + 1 = Bukser_og_Jeans, Small, Rod
Run Code Online (Sandbox Code Playgroud)

以下是Perl实现:

my %refinements = (
   Type => [
      'Toppe',
      'Bukser_og_Jeans',
   ],
   Size => [
      'Extra_small',
      'Small',
   ],
   Colour => [
      'Rod',
   ],
);

my @groups = values(%refinements);
my $iter = 0;
while (1) {
   my $num = $iter++;

   my @pick;
   for my $group (@groups) {
      my $r = $num % @$group;
      $num = ( $num - $r ) / @$group;
      push @pick, $group->[$r];
   }

   last if $num > 0;

   say join(', ', @pick);
}
Run Code Online (Sandbox Code Playgroud)

我知道它不是PHP-我不知道PHP - 但你只是问如何解决问题,不一定是代码来做,对吧?我希望你能够理解上面的Perl代码,足以解决你的问题并在PHP中重新实现它.

(如果我其实写一个Perl的解决方案,我会使用 Algorith ::循环NestedLoops.)


Por*_*ear 6

只为那些想要PHP翻译的人:

function factor_permutations($lists) {

    $permutations = array();
    $iter = 0;

    while (true) {

        $num = $iter++;
        $pick = array();

        foreach ($lists as $l) {
            $r = $num % count($l);
            $num = ($num - $r) / count($l);
            $pick[] = $l[$r];
        }

        if ($num > 0) break;
        $permutations[] = $pick;
    }

    return $permutations;
}

print_r(factor_permutations(array(array('a', 'b'), array('1', '2', '3'), array('foo', 'bar'))));
Run Code Online (Sandbox Code Playgroud)