某些业务场景下,我们会用到组合穷举方法,这里整理几个穷举方法

1、不限顺序的数组组合穷举:

  1. <?php
  2. /**
  3. * @param $arr 需要组合数组
  4. * @param int $num 组合元素个数
  5. * @return array
  6. */
  7. function getSequenceAry($arr,$num=1)
  8. {
  9. $count = count($arr);
  10. $min = min($count,$num);
  11. if($min<1){
  12. return false;
  13. }
  14. $return =array();
  15. for(;$min>=1;$min--){
  16. $arrRet = array();
  17. $max = $count-($min-1);
  18. for($i=0;$i<$max;$i++){
  19. getSequenceArySub($arr,$count,$min,$i,$arrRet,$return);
  20. }
  21. }
  22. return $return;
  23. }
  24. function getSequenceArySub($arr,$count,$min,$i,$arrRet=array(),& $return){
  25. if(empty($arr) || empty($count))
  26. return false;
  27. if(1==$min){
  28. $arrRet[--$min] = $arr[$i];
  29. $return[] = $arrRet;
  30. }else{
  31. $arrRet[--$min] = $arr[$i];
  32. for($j = $i+1;$j<($count);$j++){
  33. getSequenceArySub($arr,$count,$min,$j,$arrRet,$return);
  34. }
  35. }
  36. }
  37. print_r(getSequenceAry(array(1,2,3,4,5),1));
  38. print_r(getSequenceAry(array(1,2,3,4,5),2));
  39. print_r(getSequenceAry(array(1,2,3,4,5),3));
  40. print_r(getSequenceAry(array(1,2,3,4,5),4));
  41. print_r(getSequenceAry(array(1,2,3,4,5),5));<?php
  42. /**
  43. * @param $arr 需要组合数组
  44. * @param int $num 组合元素个数
  45. * @return array
  46. */
  47. function getSequenceAry($arr,$num=1)
  48. {
  49. $count = count($arr);
  50. $min = min($count,$num);
  51. if($min<1){
  52. return false;
  53. }
  54. $return =array();
  55. for(;$min>=1;$min--){
  56. $arrRet = array();
  57. $max = $count-($min-1);
  58. for($i=0;$i<$max;$i++){
  59. getSequenceArySub($arr,$count,$min,$i,$arrRet,$return);
  60. }
  61. }
  62. return $return;
  63. }
  64. function getSequenceArySub($arr,$count,$min,$i,$arrRet=array(),& $return){
  65. if(empty($arr) || empty($count))
  66. return false;
  67. if(1==$min){
  68. $arrRet[--$min] = $arr[$i];
  69. $return[] = $arrRet;
  70. }else{
  71. $arrRet[--$min] = $arr[$i];
  72. for($j = $i+1;$j<($count);$j++){
  73. getSequenceArySub($arr,$count,$min,$j,$arrRet,$return);
  74. }
  75. }
  76. }
  77. print_r(getSequenceAry(array(1,2,3,4,5),1));
  78. print_r(getSequenceAry(array(1,2,3,4,5),2));
  79. print_r(getSequenceAry(array(1,2,3,4,5),3));
  80. print_r(getSequenceAry(array(1,2,3,4,5),4));
  81. print_r(getSequenceAry(array(1,2,3,4,5),5));

结果:

  1. Array
  2. (
  3. [0] => Array
  4. (
  5. [0] => 1
  6. )
  7. [1] => Array
  8. (
  9. [0] => 2
  10. )
  11. [2] => Array
  12. (
  13. [0] => 3
  14. )
  15. [3] => Array
  16. (
  17. [0] => 4
  18. )
  19. [4] => Array
  20. (
  21. [0] => 5
  22. )
  23. )
  24. Array
  25. (
  26. [0] => Array
  27. (
  28. [1] => 1
  29. [0] => 2
  30. )
  31. [1] => Array
  32. (
  33. [1] => 1
  34. [0] => 3
  35. )
  36. [2] => Array
  37. (
  38. [1] => 1
  39. [0] => 4
  40. )
  41. [3] => Array
  42. (
  43. [1] => 1
  44. [0] => 5
  45. )
  46. [4] => Array
  47. (
  48. [1] => 2
  49. [0] => 3
  50. )
  51. [5] => Array
  52. (
  53. [1] => 2
  54. [0] => 4
  55. )
  56. [6] => Array
  57. (
  58. [1] => 2
  59. [0] => 5
  60. )
  61. [7] => Array
  62. (
  63. [1] => 3
  64. [0] => 4
  65. )
  66. [8] => Array
  67. (
  68. [1] => 3
  69. [0] => 5
  70. )
  71. [9] => Array
  72. (
  73. [1] => 4
  74. [0] => 5
  75. )
  76. )
  77. Array
  78. (
  79. [0] => Array
  80. (
  81. [2] => 1
  82. [1] => 2
  83. [0] => 3
  84. )
  85. [1] => Array
  86. (
  87. [2] => 1
  88. [1] => 2
  89. [0] => 4
  90. )
  91. [2] => Array
  92. (
  93. [2] => 1
  94. [1] => 2
  95. [0] => 5
  96. )
  97. [3] => Array
  98. (
  99. [2] => 1
  100. [1] => 3
  101. [0] => 4
  102. )
  103. [4] => Array
  104. (
  105. [2] => 1
  106. [1] => 3
  107. [0] => 5
  108. )
  109. [5] => Array
  110. (
  111. [2] => 1
  112. [1] => 4
  113. [0] => 5
  114. )
  115. [6] => Array
  116. (
  117. [2] => 2
  118. [1] => 3
  119. [0] => 4
  120. )
  121. [7] => Array
  122. (
  123. [2] => 2
  124. [1] => 3
  125. [0] => 5
  126. )
  127. [8] => Array
  128. (
  129. [2] => 2
  130. [1] => 4
  131. [0] => 5
  132. )
  133. [9] => Array
  134. (
  135. [2] => 3
  136. [1] => 4
  137. [0] => 5
  138. )
  139. )
  140. Array
  141. (
  142. [0] => Array
  143. (
  144. [3] => 1
  145. [2] => 2
  146. [1] => 3
  147. [0] => 4
  148. )
  149. [1] => Array
  150. (
  151. [3] => 1
  152. [2] => 2
  153. [1] => 3
  154. [0] => 5
  155. )
  156. [2] => Array
  157. (
  158. [3] => 1
  159. [2] => 2
  160. [1] => 4
  161. [0] => 5
  162. )
  163. [3] => Array
  164. (
  165. [3] => 1
  166. [2] => 3
  167. [1] => 4
  168. [0] => 5
  169. )
  170. [4] => Array
  171. (
  172. [3] => 2
  173. [2] => 3
  174. [1] => 4
  175. [0] => 5
  176. )
  177. )
  178. Array
  179. (
  180. [0] => Array
  181. (
  182. [4] => 1
  183. [3] => 2
  184. [2] => 3
  185. [1] => 4
  186. [0] => 5
  187. )
  188. )

2、穷举排列组合的函数

  1. <?php
  2. function getSequenceAry($arr)
  3. {
  4. if (count($arr) == 1) {
  5. return array($arr);
  6. }
  7. $arrRet = array();
  8. foreach ($arr as $k => $v) {
  9. $arr2 = $arr;
  10. unset($arr2[$k]);
  11. $arrOrderList = getSequenceAry($arr2);
  12. foreach ($arrOrderList as $order) {
  13. array_unshift($order, $v);
  14. $arrRet[] = $order;
  15. }
  16. }
  17. return $arrRet;
  18. }
  19. function getSequenceStr($arr)
  20. {
  21. if (count($arr) == 1) {
  22. return $arr;
  23. }
  24. $arrRet = array();
  25. foreach ($arr as $k => $v) {
  26. $arr2 = $arr;
  27. unset($arr2[$k]);
  28. $arrOrderList = getSequenceStr($arr2);
  29. foreach ($arrOrderList as $order) {
  30. $arrRet[] = $v . $order;
  31. }
  32. }
  33. return $arrRet;
  34. }
  35. print_r(getSequenceAry(array(1,2,3)));
  36. print_r(getSequenceStr(array(1,2,3)));
  37. ------------------ output ------------------
  38. Array
  39. (
  40. [0] => Array
  41. (
  42. [0] => 1
  43. [1] => 2
  44. [2] => 3
  45. )
  46. [1] => Array
  47. (
  48. [0] => 1
  49. [1] => 3
  50. [2] => 2
  51. )
  52. [2] => Array
  53. (
  54. [0] => 2
  55. [1] => 1
  56. [2] => 3
  57. )
  58. [3] => Array
  59. (
  60. [0] => 2
  61. [1] => 3
  62. [2] => 1
  63. )
  64. [4] => Array
  65. (
  66. [0] => 3
  67. [1] => 1
  68. [2] => 2
  69. )
  70. [5] => Array
  71. (
  72. [0] => 3
  73. [1] => 2
  74. [2] => 1
  75. )
  76. )
  77. Array
  78. (
  79. [0] => 123
  80. [1] => 132
  81. [2] => 213
  82. [3] => 231
  83. [4] => 312
  84. [5] => 321
  85. )