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']));
相关推荐
程序员黑豆34 分钟前
Java类型推断完全指南:从var到菱形运算符,掌握使用限制与最佳实践
java·前端·ai编程
To_OC1 小时前
踩了个 TS 的坑之后,我终于把 type 和 interface 掰明白了
前端·react.js·typescript
GreenTea1 小时前
深度解读 Anthropic 多智能体报告:更强的模型 ≠ 更好的协调
前端·后端·算法
风流 少年1 小时前
Spring AI 2.0:Memory
java·后端·spring
码匠许师傅2 小时前
【C++ 面试真题】聊聊 C++ 的移动语义与右值引用
java·c++·面试
浮生望2 小时前
前端API工程化:用 Mock 数据与 Axios 配置实现独立于后端的并行开发
前端
万少2 小时前
给 DeepSeek Harness 装个"应用商店":一条命令,595 个插件随你逛
前端·javascript·后端
小席是个热心肠2 小时前
AI相关的自我学习
java·人工智能·学习
波波0072 小时前
C# 15 重磅新特性: 带标签 break 与 continue:重新定义嵌套循环控制流
服务器·前端·c#
Brown.alexis3 小时前
es6知识点1-自备使用
前端·ecmascript·es6