-1

I'm trying to find all combinations of items in several arrays. The number of arrays is random (this can be 2, 3, 4, 5...). The number of elements in each array is random too...

For exemple, I have the 3 arrays :

$arrayA = array('A1','A2','A3',,'A4','A5','A6'); // (3 From here each time)

$arrayB = array('B1','B2','B3','B4','B5'); //(2 From here each time)

$arrayC = array('C1','C2'); // (1 From here each time)

I would like to generate an array Like :

A1+A3+A5, B1+B5, C1

A1+A4+A5, B1+B4, C1

....

I have tried like this but I need different elements number from array not ONE element from each array.

<?php

$color = array('Blue','Red','Black','Green');
$size = array('L','M','S','XL','XXL');
$type  = array('Half selevs','full seleves');
$options = [
            $color,
            $size,
            $type,
        ];
$combinations = getCombinations($options);

print_r($combinations);

function getCombinations($options){ 

            $combinations = [[]];

            for ($count = 0; $count < count($options); $count++) {
                $tmp = [];
                foreach ($combinations as $v1) {
                    foreach ($options[$count] as $v2)
                        $tmp[] = array_merge($v1, [$v2]);

                }
                $combinations = $tmp;
            }

            return $combinations;
        }
?>

Output:

Array ( [0] => Array ( [0] => Blue [1] => L [2] => Half selevs )

[1] => Array
    (
        [0] => Blue
        [1] => L
        [2] => full seleves
    )

[2] => Array
    (
        [0] => Blue
        [1] => M
        [2] => Half selevs
    )

[3] => Array
    (
        [0] => Blue
        [1] => M
        [2] => full seleves
    )

[4] => Array
    (
        [0] => Blue
        [1] => S
        [2] => Half selevs
    )

[5] => Array
    (
        [0] => Blue
        [1] => S
        [2] => full seleves
    )

[6] => Array
    (
        [0] => Blue
        [1] => XL
        [2] => Half selevs
    )

[7] => Array
    (
        [0] => Blue
        [1] => XL
        [2] => full seleves
    )

[8] => Array
    (
        [0] => Blue
        [1] => XXL
        [2] => Half selevs
    )

[9] => Array
    (
        [0] => Blue
        [1] => XXL
        [2] => full seleves
    )

[10] => Array
    (
        [0] => Red
        [1] => L
        [2] => Half selevs
    )

[11] => Array
    (
        [0] => Red
        [1] => L
        [2] => full seleves
    )

[12] => Array
    (
        [0] => Red
        [1] => M
        [2] => Half selevs
    )

[13] => Array
    (
        [0] => Red
        [1] => M
        [2] => full seleves
    )

[14] => Array
    (
        [0] => Red
        [1] => S
        [2] => Half selevs
    )

[15] => Array
    (
        [0] => Red
        [1] => S
        [2] => full seleves
    )

[16] => Array
    (
        [0] => Red
        [1] => XL
        [2] => Half selevs
    )

[17] => Array
    (
        [0] => Red
        [1] => XL
        [2] => full seleves
    )

[18] => Array
    (
        [0] => Red
        [1] => XXL
        [2] => Half selevs
    )

[19] => Array
    (
        [0] => Red
        [1] => XXL
        [2] => full seleves
    )

[20] => Array
    (
        [0] => Black
        [1] => L
        [2] => Half selevs
    )

[21] => Array
    (
        [0] => Black
        [1] => L
        [2] => full seleves
    )

[22] => Array
    (
        [0] => Black
        [1] => M
        [2] => Half selevs
    )

[23] => Array
    (
        [0] => Black
        [1] => M
        [2] => full seleves
    )

[24] => Array
    (
        [0] => Black
        [1] => S
        [2] => Half selevs
    )

[25] => Array
    (
        [0] => Black
        [1] => S
        [2] => full seleves
    )

[26] => Array
    (
        [0] => Black
        [1] => XL
        [2] => Half selevs
    )

[27] => Array
    (
        [0] => Black
        [1] => XL
        [2] => full seleves
    )

[28] => Array
    (
        [0] => Black
        [1] => XXL
        [2] => Half selevs
    )

[29] => Array
    (
        [0] => Black
        [1] => XXL
        [2] => full seleves
    )

[30] => Array
    (
        [0] => Green
        [1] => L
        [2] => Half selevs
    )

[31] => Array
    (
        [0] => Green
        [1] => L
        [2] => full seleves
    )

[32] => Array
    (
        [0] => Green
        [1] => M
        [2] => Half selevs
    )

[33] => Array
    (
        [0] => Green
        [1] => M
        [2] => full seleves
    )

[34] => Array
    (
        [0] => Green
        [1] => S
        [2] => Half selevs
    )

[35] => Array
    (
        [0] => Green
        [1] => S
        [2] => full seleves
    )

[36] => Array
    (
        [0] => Green
        [1] => XL
        [2] => Half selevs
    )

[37] => Array
    (
        [0] => Green
        [1] => XL
        [2] => full seleves
    )

[38] => Array
    (
        [0] => Green
        [1] => XXL
        [2] => Half selevs
    )

[39] => Array
    (
        [0] => Green
        [1] => XXL
        [2] => full seleves
    )

)

I want Like this:

Array ( [0] => Array ( [0] => Blue,Red,Green [1] => L,M [2] => Half selevs )

2 Answers2

0

You could do something like this...

<?php
$variants = array();
$possible_letters = array('A','B','C');
$max_variant_number = 6;

foreach ($possible_letters as $possible_letter) {
 $min_variant_number = 0;
  while ($min_variant_number <= $max_variant_number) {
    $variants[] = $possible_letters.$min_variant_number;
    $min_variant_number++;
  }
}

This is just 2 loops that do some concatenation between an array and a consistently increasing number until the number has exhausted itself and then it moves to the next letter and resets that number. Beware it is a nested loop so Order of N squared will apply here you are into complexity theory.

Nicholas Koskowski
  • 715
  • 1
  • 4
  • 18
0

Seems You need an array combination function. I found a solution here:

PHP array combinations

I copy code here for your utility, for more detail use link above.

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;
    }
}

//You must call this function foreach array you have
//with the number of elements that you want to combine
//as second parameter. Example:

$arrayA = array('A1','A2','A3','A4','A5','A6');
foreach(new Combinations($arrayA, 3) as $subarray){
    foreach($subarray as $key => $value){
        echo $value." ";
    }
    echo "\r\n";
}

Output:

A1 A2 A3 
A1 A2 A4 
A1 A2 A5 
A1 A2 A6 
A1 A3 A4 
A1 A3 A5 
A1 A3 A6 
A1 A4 A5 
A1 A4 A6 
A1 A5 A6 
A2 A3 A4 
A2 A3 A5 
A2 A3 A6 
A2 A4 A5 
A2 A4 A6 
A2 A5 A6 
A3 A4 A5 
A3 A4 A6 
A3 A5 A6 
A4 A5 A6 

Link to test it:

http://sandbox.onlinephpfunctions.com/code/e5416230aa0f9680d6990906cf80322212245e0d

Yeti82
  • 363
  • 1
  • 5
  • 14