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).
Use the composer package manager to install the Array Permutations Iterator.
composer require domwebber/array-toggle-permutations
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.
See the changelog.
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
*/
© 2021 Dom Webber