- 91浏览
- 2022-06-09
某些业务场景下,我们会用到组合穷举方法,这里整理几个穷举方法
1、不限顺序的数组组合穷举:
<?php
/**
* @param $arr 需要组合数组
* @param int $num 组合元素个数
* @return array
*/
function getSequenceAry($arr,$num=1)
{
$count = count($arr);
$min = min($count,$num);
if($min<1){
return false;
}
$return =array();
for(;$min>=1;$min--){
$arrRet = array();
$max = $count-($min-1);
for($i=0;$i<$max;$i++){
getSequenceArySub($arr,$count,$min,$i,$arrRet,$return);
}
}
return $return;
}
function getSequenceArySub($arr,$count,$min,$i,$arrRet=array(),& $return){
if(empty($arr) || empty($count))
return false;
if(1==$min){
$arrRet[--$min] = $arr[$i];
$return[] = $arrRet;
}else{
$arrRet[--$min] = $arr[$i];
for($j = $i+1;$j<($count);$j++){
getSequenceArySub($arr,$count,$min,$j,$arrRet,$return);
}
}
}
print_r(getSequenceAry(array(1,2,3,4,5),1));
print_r(getSequenceAry(array(1,2,3,4,5),2));
print_r(getSequenceAry(array(1,2,3,4,5),3));
print_r(getSequenceAry(array(1,2,3,4,5),4));
print_r(getSequenceAry(array(1,2,3,4,5),5));
结果:
Array
(
[0] => Array
(
[0] => 1
)
[1] => Array
(
[0] => 2
)
[2] => Array
(
[0] => 3
)
[3] => Array
(
[0] => 4
)
[4] => Array
(
[0] => 5
)
)
Array
(
[0] => Array
(
[1] => 1
[0] => 2
)
[1] => Array
(
[1] => 1
[0] => 3
)
[2] => Array
(
[1] => 1
[0] => 4
)
[3] => Array
(
[1] => 1
[0] => 5
)
[4] => Array
(
[1] => 2
[0] => 3
)
[5] => Array
(
[1] => 2
[0] => 4
)
[6] => Array
(
[1] => 2
[0] => 5
)
[7] => Array
(
[1] => 3
[0] => 4
)
[8] => Array
(
[1] => 3
[0] => 5
)
[9] => Array
(
[1] => 4
[0] => 5
)
)
Array
(
[0] => Array
(
[2] => 1
[1] => 2
[0] => 3
)
[1] => Array
(
[2] => 1
[1] => 2
[0] => 4
)
[2] => Array
(
[2] => 1
[1] => 2
[0] => 5
)
[3] => Array
(
[2] => 1
[1] => 3
[0] => 4
)
[4] => Array
(
[2] => 1
[1] => 3
[0] => 5
)
[5] => Array
(
[2] => 1
[1] => 4
[0] => 5
)
[6] => Array
(
[2] => 2
[1] => 3
[0] => 4
)
[7] => Array
(
[2] => 2
[1] => 3
[0] => 5
)
[8] => Array
(
[2] => 2
[1] => 4
[0] => 5
)
[9] => Array
(
[2] => 3
[1] => 4
[0] => 5
)
)
Array
(
[0] => Array
(
[3] => 1
[2] => 2
[1] => 3
[0] => 4
)
[1] => Array
(
[3] => 1
[2] => 2
[1] => 3
[0] => 5
)
[2] => Array
(
[3] => 1
[2] => 2
[1] => 4
[0] => 5
)
[3] => Array
(
[3] => 1
[2] => 3
[1] => 4
[0] => 5
)
[4] => Array
(
[3] => 2
[2] => 3
[1] => 4
[0] => 5
)
)
Array
(
[0] => Array
(
[4] => 1
[3] => 2
[2] => 3
[1] => 4
[0] => 5
)
)
2、穷举排列组合的函数
<?php
function getSequenceAry($arr)
{
if (count($arr) == 1) {
return array($arr);
}
$arrRet = array();
foreach ($arr as $k => $v) {
$arr2 = $arr;
unset($arr2[$k]);
$arrOrderList = getSequenceAry($arr2);
foreach ($arrOrderList as $order) {
array_unshift($order, $v);
$arrRet[] = $order;
}
}
return $arrRet;
}
function getSequenceStr($arr)
{
if (count($arr) == 1) {
return $arr;
}
$arrRet = array();
foreach ($arr as $k => $v) {
$arr2 = $arr;
unset($arr2[$k]);
$arrOrderList = getSequenceStr($arr2);
foreach ($arrOrderList as $order) {
$arrRet[] = $v . $order;
}
}
return $arrRet;
}
print_r(getSequenceAry(array(1,2,3)));
print_r(getSequenceStr(array(1,2,3)));
------------------ output ------------------
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => 1
[1] => 3
[2] => 2
)
[2] => Array
(
[0] => 2
[1] => 1
[2] => 3
)
[3] => Array
(
[0] => 2
[1] => 3
[2] => 1
)
[4] => Array
(
[0] => 3
[1] => 1
[2] => 2
)
[5] => Array
(
[0] => 3
[1] => 2
[2] => 1
)
)
Array
(
[0] => 123
[1] => 132
[2] => 213
[3] => 231
[4] => 312
[5] => 321
)
版权声明:
1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。
2、本站仅提供信息发布平台,不承担相关法律责任。
3、若侵犯您的版权或隐私,请联系本站管理员删除。
4、本文由会员转载自互联网,如果您是文章原创作者,请联系本站注明您的版权信息。