portavice/排列

生成数组的正常和递归排列

1.2.2 2023-08-07 11:14 UTC

This package is auto-updated.

Last update: 2024-09-09 11:34:46 UTC


README

Latest Version on Packagist Test Status Code Style Status PHP from Packagist Total Downloads

这是一个简单的PHP排列库。

它可以用来生成给定数组所有可能的排列。

它也可以用来递归地生成给定数组所有可能的排列。

安装

使用 Composer 安装此包

要安装它,只需将以下内容添加到您的 composer.json 文件中

composer require portavice/permutation

方法

用法

<?php
require_once 'vendor/autoload.php';

use Portavice\Permutation\Permutation;

// You can also use the static method:
$permutations = Permutation::getPermutations(
    [
        'a' => ['a1', 'a2'],
        'b' => ['b1', 'b2'],
        'c' => ['c1', 'c2'], 
    ]
);
// Output:
// [
//     ['a' => 'a1', 'b' => 'b1', 'c' => 'c1'],
//     ['a' => 'a1', 'b' => 'b1', 'c' => 'c2'],
//     ['a' => 'a1', 'b' => 'b2', 'c' => 'c1'],
//     ['a' => 'a1', 'b' => 'b2', 'c' => 'c2'],
//     ['a' => 'a2', 'b' => 'b1', 'c' => 'c1'],
//     ['a' => 'a2', 'b' => 'b1', 'c' => 'c2'],
//     ['a' => 'a2', 'b' => 'b2', 'c' => 'c1'],
//     ['a' => 'a2', 'b' => 'b2', 'c' => 'c2'],
// ]

// You can also use the recursive method:
$permutations = Permutation::getPermutationsRecursive(
    [
        'a' => ['a1', 'a2'],
        'b' => ['b1', 'b2'],
        'c' => ['c1', 'c2'], 
    ]
);
// Output:
// [
//     ['a' => 'a1'],
//     ['a' => 'a2'],
//     ['b' => 'b1'],
//     ['b' => 'b2'],
//     ['c' => 'c1'],
//     ['c' => 'c2'],
//     ['a' => 'a1', 'b' => 'b1'],
//     ['a' => 'a1', 'b' => 'b2'],
//     ['a' => 'a1', 'c' => 'c1'],
//     ['a' => 'a1', 'c' => 'c2'],
//     ['a' => 'a2', 'b' => 'b1'],
//     ['a' => 'a2', 'b' => 'b2'],
//     ['a' => 'a2', 'c' => 'c1'],
//     ['a' => 'a2', 'c' => 'c2'],
//     ['b' => 'b1', 'c' => 'c1'],
//     ['b' => 'b1', 'c' => 'c2'],
//     ['b' => 'b2', 'c' => 'c1'],
//     ['b' => 'b2', 'c' => 'c2'],
//     ['a' => 'a1', 'b' => 'b1', 'c' => 'c1'],
//     ['a' => 'a1', 'b' => 'b1', 'c' => 'c2'],
//     ['a' => 'a1', 'b' => 'b2', 'c' => 'c1'],
//     ['a' => 'a1', 'b' => 'b2', 'c' => 'c2'],
//     ['a' => 'a2', 'b' => 'b1', 'c' => 'c1'],
//     ['a' => 'a2', 'b' => 'b1', 'c' => 'c2'],
//     ['a' => 'a2', 'b' => 'b2', 'c' => 'c1'],
//     ['a' => 'a2', 'b' => 'b2', 'c' => 'c2'],
// ]

// NEW: Generators! For memory constrained environments.
// More info how to use generators here: https://php.ac.cn/manual/en/language.generators.overview.php
$permutations = Permutation::getGenerator([
    'a' => ['a1', 'a2'],
    'b' => ['b1', 'b2'],
    'c' => ['c1', 'c2'],
]);

foreach ($permutations as $permutation) {
    // ... do stuff here!
}

许可证

此库根据MIT许可证授权。

作者

此库由 Shaun LüdekePortavice GmbH 编写。

开发

如何开发

  • 运行 composer install 以安装PHP的依赖项。
  • 运行 composer test 以运行所有PHPUnit测试。
  • 运行 composer cs 以检查代码风格合规性,并在每次提交前运行 composer csfix 以修复代码风格违规。

代码风格

PHP代码必须遵循 PSR-12规范

我们使用 PHP_CodeSniffer 进行PHP代码风格检查。