PHP Array Combinations

PHP array combinations

You can use the solution found here http://stereofrog.com/blok/on/070910.

Incase the link goes down here's the code....

class Combinations implements Iterator
{
protected $c = null;
protected $s = null;
protected $n = 0;
protected $k = 0;
protected $pos = 0;

function __construct($s, $k) {
if(is_array($s)) {
$this->s = array_values($s);
$this->n = count($this->s);
} else {
$this->s = (string) $s;
$this->n = strlen($this->s);
}
$this->k = $k;
$this->rewind();
}
function key() {
return $this->pos;
}
function current() {
$r = array();
for($i = 0; $i < $this->k; $i++)
$r[] = $this->s[$this->c[$i]];
return is_array($this->s) ? $r : implode('', $r);
}
function next() {
if($this->_next())
$this->pos++;
else
$this->pos = -1;
}
function rewind() {
$this->c = range(0, $this->k);
$this->pos = 0;
}
function valid() {
return $this->pos >= 0;
}

protected function _next() {
$i = $this->k - 1;
while ($i >= 0 && $this->c[$i] == $this->n - $this->k + $i)
$i--;
if($i < 0)
return false;
$this->c[$i]++;
while($i++ < $this->k - 1)
$this->c[$i] = $this->c[$i - 1] + 1;
return true;
}
}


foreach(new Combinations("1234567", 5) as $substring)
echo $substring, ' ';

12345 12346 12347 12356 12357 12367 12456 12457 12467 12567 13456 13457 13467 13567 14567 23456 23457 23467 23567 24567 34567

PHP algorithm to generate all combinations of a specific size from a single set

I would use a recursive function. Here's a (working) example with comments. Hope this works for you!

function sampling($chars, $size, $combinations = array()) {

# if it's the first iteration, the first set
# of combinations is the same as the set of characters
if (empty($combinations)) {
$combinations = $chars;
}

# we're done if we're at size 1
if ($size == 1) {
return $combinations;
}

# initialise array to put new values in
$new_combinations = array();

# loop through existing combinations and character set to create strings
foreach ($combinations as $combination) {
foreach ($chars as $char) {
$new_combinations[] = $combination . $char;
}
}

# call same function again for the next iteration
return sampling($chars, $size - 1, $new_combinations);

}

// example
$chars = array('a', 'b', 'c');
$output = sampling($chars, 2);
var_dump($output);
/*
array(9) {
[0]=>
string(2) "aa"
[1]=>
string(2) "ab"
[2]=>
string(2) "ac"
[3]=>
string(2) "ba"
[4]=>
string(2) "bb"
[5]=>
string(2) "bc"
[6]=>
string(2) "ca"
[7]=>
string(2) "cb"
[8]=>
string(2) "cc"
}
*/

PHP Find All (somewhat) Unique Combinations of an Array

If you don't mind using a couple of global variables, you could do this in PHP (translated from a version in JavaScript):

<?PHP
$result = array();
$combination = array();

function combinations(array $myArray, $choose) {
global $result, $combination;

$n = count($myArray);

function inner ($start, $choose_, $arr, $n) {
global $result, $combination;

if ($choose_ == 0) array_push($result,$combination);
else for ($i = $start; $i <= $n - $choose_; ++$i) {
array_push($combination, $arr[$i]);
inner($i + 1, $choose_ - 1, $arr, $n);
array_pop($combination);
}
}
inner(0, $choose, $myArray, $n);
return $result;
}

print_r(combinations(array(20,20,22,24), 3));
?>

OUTPUT:

Array ( [0] => Array ( [0] => 20 
[1] => 20
[2] => 22 )
[1] => Array ( [0] => 20
[1] => 20
[2] => 24 )
[2] => Array ( [0] => 20
[1] => 22
[2] => 24 )
[3] => Array ( [0] => 20
[1] => 22
[2] => 24 ) )

Generate all possible combinations with PHP

This is probably not very efficient but it will get the job done:

<?php
$alph = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
$a = array();

foreach( $alph as $l1 )
{
$a[0] = $l1.'10';
foreach( $alph as $l2 )
{
$a[1] = $l2.'1';
foreach( $alph as $l3 )
{
$a[2] = $l3.'18';
foreach( $alph as $l4 )
{
$a[3] = $l4.'4';
foreach( $alph as $l5 )
{
$a[4] = $l5.'9';
foreach( $alph as $l6 )
{
$a[5] = $l6.'14';
foreach( $alph as $l7 )
{
$a[6] = $l7;
foreach( $alph as $l8 )
{
$a[7] = ' '.$l8; // your example shows a space, not sure if that was intentional
// $line = implode( '', $a )."\r\n";
// Write $line to a file
}
}
}
}
}
}
}
}

Sample output:

A10A1A18A4A9A14A A
A10A1A18A4A9A14A B
A10A1A18A4A9A14A C
A10A1A18A4A9A14A D
A10A1A18A4A9A14A E
A10A1A18A4A9A14A F
A10A1A18A4A9A14A G
A10A1A18A4A9A14A H
A10A1A18A4A9A14A I
A10A1A18A4A9A14A J
A10A1A18A4A9A14A K
A10A1A18A4A9A14A L
A10A1A18A4A9A14A M
A10A1A18A4A9A14A N
A10A1A18A4A9A14A O
A10A1A18A4A9A14A P
A10A1A18A4A9A14A Q
A10A1A18A4A9A14A R
A10A1A18A4A9A14A S
A10A1A18A4A9A14A T
A10A1A18A4A9A14A U
A10A1A18A4A9A14A V
A10A1A18A4A9A14A W
A10A1A18A4A9A14A X
A10A1A18A4A9A14A Y
A10A1A18A4A9A14A Z
A10A1A18A4A9A14B A
A10A1A18A4A9A14B B
A10A1A18A4A9A14B C
A10A1A18A4A9A14B D
A10A1A18A4A9A14B E
A10A1A18A4A9A14B F
A10A1A18A4A9A14B G
A10A1A18A4A9A14B H
A10A1A18A4A9A14B I
A10A1A18A4A9A14B J
A10A1A18A4A9A14B K
A10A1A18A4A9A14B L
A10A1A18A4A9A14B M
A10A1A18A4A9A14B N
A10A1A18A4A9A14B O
A10A1A18A4A9A14B P
A10A1A18A4A9A14B Q
A10A1A18A4A9A14B R
A10A1A18A4A9A14B S
A10A1A18A4A9A14B T
A10A1A18A4A9A14B U
A10A1A18A4A9A14B V
A10A1A18A4A9A14B W
A10A1A18A4A9A14B X
A10A1A18A4A9A14B Y
A10A1A18A4A9A14B Z
A10A1A18A4A9A14C A
A10A1A18A4A9A14C B
A10A1A18A4A9A14C C
A10A1A18A4A9A14C D
A10A1A18A4A9A14C E
A10A1A18A4A9A14C F
A10A1A18A4A9A14C G
A10A1A18A4A9A14C H

How to generate in PHP all combinations of items in multiple arrays

Here is recursive solution:

function combinations($arrays, $i = 0) {
if (!isset($arrays[$i])) {
return array();
}
if ($i == count($arrays) - 1) {
return $arrays[$i];
}

// get combinations from subsequent arrays
$tmp = combinations($arrays, $i + 1);

$result = array();

// concat each array from tmp with each element from $arrays[$i]
foreach ($arrays[$i] as $v) {
foreach ($tmp as $t) {
$result[] = is_array($t) ?
array_merge(array($v), $t) :
array($v, $t);
}
}

return $result;
}

print_r(
combinations(
array(
array('A1','A2','A3'),
array('B1','B2','B3'),
array('C1','C2')
)
)
);

All combinations of r elements from given array php

A combination can be expressed as

nCr = n! / (r! - (n - r)!)

First, we determine $n as the number of elements in the array. And $r is the minimum number of elements in each combination.

$a = ['1', '2', '3', '4', '5', '6', '7'];  // the array of elements we are interested in

// Determine the `n` and `r` in nCr = n! / (r! * (n-r)!)
$r = 5;
$n = count($a);

Next, we determine $max as the maximum number that can be represented by $n binary digits. That is, if $n = 3, then $max = (111)2 = 7. To do this, we first create a empty string $maxBinary and add $n number of 1s to it. We then convert it to decimal, and store it in $max.

$maxBinary = "";
for ($i = 0; $i < $n; $i++)
{
$maxBinary .= "1";
}
$max = bindec($maxBinary); // convert it into a decimal value, so that we can use it in the following for loop

Then, we list out every binary number from 0 to $max and store those that have more than $r number of 1s in them.

$allBinary = array();  // the array of binary numbers
for ($i = 0; $i <= $max; $i++)
{
if (substr_count(decbin($i), "1") >= $r) // we count the number of ones to determine if they are >= $r
{
// we make the length of the binary numbers equal to the number of elements in the array,
// so that it is easy to select elements from the array, based on which of the digits are 1.
// we do this by padding zeros to the left.
$temp = str_pad(decbin($i), $n, "0", STR_PAD_LEFT);
$allBinary[] = $temp;
}
}

Then, we use the same trick as above to select elements for our combination. I believe the comments explain enough.

$combs = array();  // the array for all the combinations.
$row = array(); // the array of binary digits in one element of the $allBinary array.

foreach ($allBinary as $key => $one)
{
$combs[$key] = "";
$row = str_split($one); // we store the digits of the binary number individually
foreach ($row as $indx => $digit)
{
if ($digit == '1') // if the digit is 1, then the corresponding element in the array is part of this combination.
{
$combs[$key] .= $a[$indx]; // add the array element at the corresponding index to the combination
}
}
}

And that is it. You are done!

Now if you have something like

echo count($combs);

then it would give you 29.

Additional notes:

I read up on this only after seeing your question, and as a newcomer, I found these useful:

  • Wikipedia - http://en.wikipedia.org/wiki/Combination
  • Php recursion to get all possibilities of strings
  • Algorithm to return all combinations of k elements from n

Also, here are some quick links to the docs, that should help people who see this in the future:

  • http://php.net/manual/en/function.decbin.php
  • http://php.net/manual/en/function.bindec.php
  • http://php.net/manual/en/function.str-pad.php

Generating unique combinations in PHP

Say you have a set of 4 items and you want a random subset of 3 of them. You can do the following:

$myset = [ "A","B","C", "D" ];    

function randomSubset($set, $size) {
$array = array_pad(array_pad([],$size,1),count($set),0); //Get an array like [ 1,1,1,0 ];
shuffle($array);
return array_intersect_key($set, array_filter($array));
}

print_r(randomSubset($myset,3));

See it at a sandbox: http://sandbox.onlinephpfunctions.com/code/a68f3b2f1abc285424ccadbaab8a12fc4928bb8d

Now if you need all subsets of size N you can do some recursive magic:



function allSubsets($set, $size) {     
$subsets = [];
if ($size == 1) {
return array_map(function ($v) { return [$v]; },$set);
}
foreach (allSubsets($set,$size-1) as $subset) {
foreach ($set as $element) {
if (!in_array($element,$subset)) {
$newSet = array_merge($subset,[$element]);
sort($newSet);
if (!in_array($newSet,$subsets)) {
$subsets[] = array_merge($subset,[$element]);
}
}
}
}
return $subsets;

}

$myset = [ "A","B","C", "D", "E" ];
print_r(allSubsets($myset,3));

Sandboxed: http://sandbox.onlinephpfunctions.com/code/01b55f0c1eb55ee82a67175d2551c164ffb2bf87

Get all possible combinations without duplicates

I hope below function work as per your expected output :

function get_array_combination($arr) {
$results = array(array( ));

foreach ($arr as $values)
foreach ($results as $combination)
array_push($results, array_merge($combination, array($values))); // Get new values and merge to your previous combination. And push it to your results array
return $results;
}
$set = array('1', '2', '3', '4');
$final_array = get_array_combination($set);
echo "<pre>";
print_r(array_values(array_filter($final_array))); // Removed blank entry from array and re indexing array keys.


Related Topics



Leave a reply



Submit