Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP array combinations

I have an array of 7 numbers (1,2,3,4,5,6,7) and I want to choose 5 of the numbers like
(1,2,3,4,5), (1,2,3,4,6), (1,2,3,4,7).
Note that (1,2,3,4,5) is equal to (4,5,3,1,2), so only one of those should be included in the output.

I would like to know if there is a function in PHP or any algorithm that can do this ? I have no idea where to start from. Can you help me ?

I want all the combinations of 7 given numbers ( they are taken from an array ) put into 5 slots, disregarding order.

like image 328
NVG Avatar asked Sep 18 '10 16:09

NVG


People also ask

Which PHP function is used to combine arrays?

The array_merge() function merges one or more arrays into one array. Tip: You can assign one array to the function, or as many as you like.

What are the three types of array in PHP?

In PHP, there are three types of arrays: Indexed arrays - Arrays with a numeric index. Associative arrays - Arrays with named keys. Multidimensional arrays - Arrays containing one or more arrays.

What is PHP array with example?

An array is a data structure that stores one or more similar type of values in a single value. For example if you want to store 100 numbers then instead of defining 100 variables its easy to define an array of 100 length.


1 Answers

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

like image 77
Galen Avatar answered Sep 23 '22 18:09

Galen