ArrayCombinations
ArrayCombinations::subsets($array, $minLength = 1) enumerates subsets of a zero-based consecutive list. Each result is itself a list.
use Derafu\Array\ArrayCombinations;
ArrayCombinations::subsets([1, 2, 3]);
// [[1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]
ArrayCombinations::subsets([1, 2, 3], 2);
// [[1, 2], [1, 3], [2, 3], [1, 2, 3]]
ArrayCombinations::subsets([1, 2], 0);
// [[], [1], [2], [1, 2]]
Selection is by position, preserving the input order within each subset. Equal values at different positions can therefore produce equal-looking subsets. The input is unchanged.
The algorithm examines 2^n combinations. By default it excludes the empty subset, returning 2^n - 1 results; a minimum length of zero includes it. Use small lists: raising the minimum length filters results but does not avoid examining the combinations.
On this page
Last updated on 26/10/2026
by Anonymous