PHP 排列组合

几个排列组合例子:【递归】

一、出处:对数组作排列组合的PHP算法 | maraboy

function array_comb($array, $n) {
    $results = [];
    // case $n == 1, give straightforward result
    if ($n == 1) {
        foreach ($array as $key => $value)
            $results[] = array($key => $value);
        return $results;
    }
    // case $n > 1
    // sort the array by key to make key comparison possible later
    ksort($array);
    // get combination results for n - 1 array, where m is count of array elements
    $subarray = array_slice($array, 1, null, true);
    $subarray_results = array_comb($subarray, $n - 1);
    // cross merge the m array elements with n - 1 array results
    foreach ($array as $key => $value)
        foreach ($subarray_results as $subarray_result)
            if ($key < array_shift(array_keys($subarray_result)))
                $results[] = array($key => $value) + $subarray_result;
    return $results;
}

执行结果如下:

# 假设执行 array_comb(['[1:1]', '[2:2]', '[3:3]'], 2); 并 var_dump 出来
array (size=3)
  0 => 
    array (size=2)
      0 => string '[1:1]' (length=5)
      1 => string '[2:2]' (length=5)
  1 => 
    array (size=2)
      0 => string '[1:1]' (length=5)
      2 => string '[3:3]' (length=5)
  2 => 
    array (size=2)
      1 => string '[2:2]' (length=5)
      2 => string '[3:3]' (length=5)

# 将 return 前一行修改一下,即
$results[] = array($key => $value) + $subarray_result;
# 修改为
$results[] = $value . ',' . $subarray_result[key($subarray_result)];
#-------------------------------------------------------------------#
# 得到另一种结果:
array (size=3)
  0 => string '[1:1],[2:2]' (length=11)
  1 => string '[1:1],[3:3]' (length=11)
  2 => string '[2:2],[3:3]' (length=11)

二、

function getCombinArrayByLength($arr, $len, $t = []) {
    if ($len == 0) {
        return [$t];
    }
    $result = [];
    for ($i = 0, $l = count($arr); $i <= $l - $len; $i++) {
        $tmp = getCombinArrayByLength(array_slice($arr, $i + 1, $l, false), $len - 1, array_merge($t, array($arr[$i])));
        $result = array_merge($result, $tmp);
    }
    return $result;
}

执行结果如下:

0 => 
  array (size=2)
    0 => string '[1:1]' (length=5)
    1 => string '[2:2]' (length=5)
1 => 
  array (size=2)
    0 => string '[1:1]' (length=5)
    1 => string '[3:3]' (length=5)
2 => 
  array (size=2)
    0 => string '[2:2]' (length=5)
    1 => string '[3:3]' (length=5)

三、

function getCombinationToString($arr, $m) {
    $result = array();
    if ($m == 1) {
        return $arr;
    }
    if ($m == count($arr)) {
        $result[] = implode(',', $arr);
        return $result;
    }
    $tmp = $arr[0];
    unset($arr[0]);
    $arr = array_values($arr);
    $temp_list1 = getCombinationToString($arr, ($m - 1));
    foreach ($temp_list1 as $s) {
        $s = $tmp . ',' . $s;
        $result[] = $s;
    }
    unset($temp_list1);
    $temp_list2 = getCombinationToString($arr, $m);
    foreach ($temp_list2 as $s) {
        $result[] = $s;
    }
    unset($temp_list2);
    return $result;
}

执行结果如下:

# 直接就是组合字符串
array (size=3)
  0 => string '[1:1],[2:2]' (length=11)
  1 => string '[1:1],[3:3]' (length=11)
  2 => string '[2:2],[3:3]' (length=11)

阅读更多
版权声明:Max Sky 原创文章,转载时请保留所有权并以超链接形式标明文章出处。否则将追究相关法律责任。 https://blog.csdn.net/maxsky/article/details/80349563
文章标签: PHP 排列 组合 递归
个人分类: Web
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭