【问题标题】:PHP array combination with order带顺序的PHP数组组合
【发布时间】:2015-01-18 20:22:48
【问题描述】:

我试图弄清楚如何组合两个数组,同时在 PHP 中保持每个单独数组的顺序。

这是一个例子

$array1=array("a","b","c","d"); a 需要在 b 之前,b 需要在 c 之前,c 需要在 d 之前 $array2=array("h","g","f","e"); h需要在g之前,g需要在f之前,f需要在e之前

所有元素都需要在那里,因此输出将始终包含所有元素的总和。

最后,我想得到所有的组合:

  • h,g,f,e,a,b,c,d
  • h,g,f,a,e,b,c,d
  • h,g,f,a,b,e,c,d
  • h,g,f,a,b,c,e,d
  • h,g,f,a,b,c,d,e

  • h,g,a,f,e,b,c,d
  • h,g,a,f,b,e,c,d
  • h,g,a,f,b,c,e,d
  • h,g,a,f,b,c,d,e

  • h,g,a,b,f,e,c,d
  • h,g,a,b,f,c,e,d
  • h,g,a,b,f,c,d,e

...等等...

我在这个网站上找到了很多组合示例,但没有一个输出应该保持初始顺序。

你知道我该怎么做吗?我可以迭代地做,但考虑到所有的组合,这显然不是最好的方法。

谢谢 洛朗

【问题讨论】:

  • 我不太明白你的问题。您想保留订单,但还想要所有可能的订单组合?
  • 我希望将数组与所有可能的组合混合,但应该尊重初始数组的顺序。下面的地狱代码找到了解决方案。谢谢

标签: php combinations


【解决方案1】:

这是我对脚本的建议:

<?php

  $array1 = array("a","b","c","d");
  $array2 = array("h","g","f","e");
  // produce 8-byte-string with each 4 values of array 1 and 2
  // looks like a byte with 8 bits of value 0 and 1
  // and delete combinations with other than 4 zeroes and 4 ones
  $combos = array();
  for($i=0; $i<256; $i++) {
    $str = decbin($i);
    $str = substr("00000000",0,8-strlen($str)).$str;
    // just check if there are 4 ones in the string
    if(strlen(str_replace("0","",$str))==4) {
      // then add it to combos array
      $combos[$str] = array();
      // echo it if you like
      //echo "<br>".$i.": ".$str;
    }
  }
  # now you have all combinations
  # so you can fill it with the array values
  foreach($combos as $key => $array) {
    $array = array();
    $x1=0;
    $x2=0;
    for($i=0; $i<strlen($key); $i++) {
      if(substr($key,$i,1) == "1") {
        $array[] = $array1[$x1];
        $x1++;
      } else {
        $array[] = $array2[$x2];
        $x2++;
      }
    }
    // echo it if you like
    echo "<br>".$key.": ".implode(", ",$array);
    $combos[$key] = $array;
  }
  echo "<br /><br />Combinations: ".count($combos);

?>

这是输出:

00001111: h, g, f, e, a, b, c, d
00010111: h, g, f, a, e, b, c, d
00011011: h, g, f, a, b, e, c, d
00011101: h, g, f, a, b, c, e, d
00011110: h, g, f, a, b, c, d, e
00100111: h, g, a, f, e, b, c, d
00101011: h, g, a, f, b, e, c, d
00101101: h, g, a, f, b, c, e, d
00101110: h, g, a, f, b, c, d, e
00110011: h, g, a, b, f, e, c, d
00110101: h, g, a, b, f, c, e, d
00110110: h, g, a, b, f, c, d, e
00111001: h, g, a, b, c, f, e, d
00111010: h, g, a, b, c, f, d, e
00111100: h, g, a, b, c, d, f, e
01000111: h, a, g, f, e, b, c, d
01001011: h, a, g, f, b, e, c, d
01001101: h, a, g, f, b, c, e, d
01001110: h, a, g, f, b, c, d, e
01010011: h, a, g, b, f, e, c, d
01010101: h, a, g, b, f, c, e, d
01010110: h, a, g, b, f, c, d, e
01011001: h, a, g, b, c, f, e, d
01011010: h, a, g, b, c, f, d, e
01011100: h, a, g, b, c, d, f, e
01100011: h, a, b, g, f, e, c, d
01100101: h, a, b, g, f, c, e, d
01100110: h, a, b, g, f, c, d, e
01101001: h, a, b, g, c, f, e, d
01101010: h, a, b, g, c, f, d, e
01101100: h, a, b, g, c, d, f, e
01110001: h, a, b, c, g, f, e, d
01110010: h, a, b, c, g, f, d, e
01110100: h, a, b, c, g, d, f, e
01111000: h, a, b, c, d, g, f, e
10000111: a, h, g, f, e, b, c, d
10001011: a, h, g, f, b, e, c, d
10001101: a, h, g, f, b, c, e, d
10001110: a, h, g, f, b, c, d, e
10010011: a, h, g, b, f, e, c, d
10010101: a, h, g, b, f, c, e, d
10010110: a, h, g, b, f, c, d, e
10011001: a, h, g, b, c, f, e, d
10011010: a, h, g, b, c, f, d, e
10011100: a, h, g, b, c, d, f, e
10100011: a, h, b, g, f, e, c, d
10100101: a, h, b, g, f, c, e, d
10100110: a, h, b, g, f, c, d, e
10101001: a, h, b, g, c, f, e, d
10101010: a, h, b, g, c, f, d, e
10101100: a, h, b, g, c, d, f, e
10110001: a, h, b, c, g, f, e, d
10110010: a, h, b, c, g, f, d, e
10110100: a, h, b, c, g, d, f, e
10111000: a, h, b, c, d, g, f, e
11000011: a, b, h, g, f, e, c, d
11000101: a, b, h, g, f, c, e, d
11000110: a, b, h, g, f, c, d, e
11001001: a, b, h, g, c, f, e, d
11001010: a, b, h, g, c, f, d, e
11001100: a, b, h, g, c, d, f, e
11010001: a, b, h, c, g, f, e, d
11010010: a, b, h, c, g, f, d, e
11010100: a, b, h, c, g, d, f, e
11011000: a, b, h, c, d, g, f, e
11100001: a, b, c, h, g, f, e, d
11100010: a, b, c, h, g, f, d, e
11100100: a, b, c, h, g, d, f, e
11101000: a, b, c, h, d, g, f, e
11110000: a, b, c, d, h, g, f, e

Combinations: 70

【讨论】:

  • 地狱代码,这太棒了!正是我需要的和优雅的解决方案!非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-17
  • 1970-01-01
  • 2019-05-08
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 2017-07-29
相关资源
最近更新 更多