php 数组中的元素进行排列组合

需求背景:计算出数组['A','B','C','D']各种排列组合,希望得到的是数据如下图

直接上代码:

php 复制代码
 private function finish_combination($array, &$groupResult = [], $splite = ',')
    {
        $result = [];
        $finish_result = [];
        $this->diffArrayItems($array, $result);
        foreach ($result as $value) {
            $items = $this->array_combination($value, $splite);
            if (!isset($groupResult[count($value)])) $groupResult[count($value)] = [];
            $groupResult[count($value)] = array_merge($groupResult[count($value)], $items);
            $finish_result = array_merge($finish_result, $items);
        }
        return $finish_result;
    }
// 不同数组的值进行排列组合例如:['A','B','C'],排列如何出所有的可能性
   private function array_combination($arr, $splite = ',')
    {
        $len = count($arr);
        if ($len == 1) {
            return $arr;
        }
        $result = array();
        for ($i = 0; $i < $len; $i++) {
            $tmp_arr = $arr;
            unset($tmp_arr[$i]);
            $tmp_arr = array_values($tmp_arr);
            $tmp_result = $this->array_combination($tmp_arr, $splite);
            foreach ($tmp_result as $val) {
                $val .= $splite . $arr[$i];
                $result[] = $val;

            }
        }
        return $result;
    }
 private function getCombinations($input, $length, $start = 0, $current = array(), &$result = array())
    {
        if (count($current) === $length) {
            $result[] = $current;
            return;
        }

        for ($i = $start; $i < count($input); $i++) {
            $current[] = $input[$i];
            $this->getCombinations($input, $length, $i + 1, $current, $result);
            array_pop($current);
        }
    }
// 获取不同个数的数组例如['A'],['B'],['C'],['D'],['A''B'],['A''c']......['A', 'B', 'C', 'D']

private function diffArrayItems($input, &$result)
    {
        for ($i = 1; $i <= count($input); $i++) {
            $this->getCombinations($input, $i, 0, array(), $result);
        }
    }

//直接调用
$result = $this->finish_combination(['A', 'B', 'C', 'D']);
var_dump($result);

// 排列组合了所有的数据后,一般情况下,我们都想着验证下个数是否正确,

例如:

取 1 个元素的排列组合数为 P(4, 1) = 4! / (4 - 1)! = 4! / 3! = 4

取 2 个元素的排列组合数为 P(4, 2) = 4! / (4 - 2)! = 4! / 2! = 12

取 3 个元素的排列组合数为 P(4, 3) = 4! / (4 - 3)! = 4! / 1! = 24

取 4 个元素的排列组合数为 P(4, 4) = 4! / (4 - 4)! = 4! / 0! = 24

下面就需要另外一个函数

php 复制代码
//此方法需要开启GMP扩展
private function nP($n)
    {
        $sum = 0;
        for ($i = 1; $i <= $n; $i++) {
            $sum += gmp_fact($n) / gmp_fact($n - $i);
        }
        return intval($sum);
    }
// 然后直接调用
echo $this->nP(count('A', 'B', 'C', 'D']));
相关推荐
小码哥_常24 分钟前
从MVC到MVI:一文吃透架构模式进化史
前端
嗷o嗷o25 分钟前
Android BLE 的 notify 和 indicate 到底有什么区别
前端
豹哥学前端29 分钟前
别再背“var 提升,let/const 不提升”了:揭开暂时性死区的真实面目
前端·面试
小雅痞33 分钟前
[Java][Leetcode simple] 28. 找出字符串中第一个匹配项的下标
java·开发语言·leetcode
likerhood38 分钟前
java中的不可变类(Immutable)
java·开发语言
lar_slw1 小时前
k8s部署前端项目
前端·容器·kubernetes
拉拉肥_King1 小时前
Ant Design Table 横向滚动条神秘消失?我是如何一步步找到真凶的
前端·javascript
GreenTea1 小时前
DeepSeek-V4 技术报告深度分析:基础研究创新全景
前端·人工智能·后端
yaoxin5211231 小时前
389. Java IO API - 获取文件名
java·开发语言·python
Wang15302 小时前
Java排序
java