domwebber/array-toggle-permutations

PHP array permutations iterator. A simple way to loop through every possible combination of items in an array.

v1.0.2 2021-07-12 21:55 UTC

This package is auto-updated.

Last update: 2024-11-13 05:21:20 UTC


README

PRs Welcome Latest Stable Version Total Downloads License

A helpful PHP utility that easily loops through every possible combination of array items. This package is intended for use in testing environments but is unit tested to the degree that it is also safe for use in a production environment too (within reason).

Installation

Use the composer package manager to install the Array Permutations Iterator.

composer require domwebber/array-toggle-permutations

Usage

This library can be used to toggle the options of a numerically-indexed array or an associative array. In the case where an associative array is used, the array keys will be maintained. In either case, the array items will stay in the original order.

Basic usage:

use Dw\Utils\ArrayTogglePermutations;

$iterator = new ArrayTogglePermutations(
    [0,1,2]
);

// $iterator = new ArrayTogglePermutations(...);
foreach ($iterator as $permutation) {
    // Do something with the combination... For example:
    var_dump($permutation);
}

See examples.

Contributing

See the changelog.

File Signatures

Wherever possible, files should include a file signature comment.

/**
 * PHP Array Permutations Iterator.
 *
 * @since 1.0.0
 * @package Dw\Utils
 * @copyright 2021 Dom Webber <dom.webber@hotmail.com>
 * @link https://github.com/domwebber
 */

License

GNU GPL v3.0

© 2021 Dom Webber