Jump to content

Recommended Posts

I am looking for help to make a function who returns the all possible variations without repetitions of the 2D array elements. I have done it easily with 1D array like:


$array = array('a', 'b', 'c', 'd');
print_r(variations($array));

function variations($array){
$size = count($array);
$data = array();
for($i=0; $i<$size; $i++)	{
	foreach($array as $key=>$val)
		$data[] = array_slice($array, 0, $key+1);
array_shift($array);
}
return $data;
};

 

Have no luck to do so with 2D array what is required for the certain part of the web. The result must look something like:

 

$array = array(
                        array('a1', 'a2', 'a3'),
                        array('b1', 'b2', 'b3'),
                        array('c1', 'c2', 'c3'),
                                      ...
                                      ...
                        array('x1', 'x2', 'x3')
                       );


################################# 1 variable
a1
a2
a3

b1
b2
b3

c1
c2
c3

########################################################## 2 variables
a1		b1
a1		b2
a1		b3

a2		b1
a2		b2
a2		b3

a3		b1
a3		b2
a3		b3	
-----------------
a1		c1
a1		c2
a1		c3

a2		c1
a2		c2
a2		c3

a3		c1
a3		c2
a3		c3
-----------------------------------------
			b1		c1
			b1		c2
			b1		c3

			b2		c1
			b2		c2
			b2		c3

			b3		c1
			b3		c2
			b3		c3	
############################################################ 3 variables
a1		b1		c1
a1		b2		c1
a1		b3		c1


a1		b2		c1
a1		b2		c2
a1		b2		c3

a1		b3		c1
a1		b3		c2
a1		b3		c3
-------------------------------------------------------------
				a2		b1		c1
				a2		b2		c1
				a2		b3		c1


				a2		b2		c1
				a2		b2		c2
				a2		b2		c3

				a2		b3		c1
				a2		b3		c2
				a2		b3		c3
				------------------------------------------------------------
									a3		b1		c1
									a3		b2		c1
									a3		b3		c1


									a3		b2		c1
									a3		b2		c2
									a3		b2		c3

									a3		b3		c1
									a3		b3		c2
									a3		b3		c3

                                             --------------------------------------------------------------

############################################################## n variables

############################################################## n variables

 

Any help and advices would be appreciated. Thank you

Link to comment
https://forums.phpfreaks.com/topic/242632-2d-array-variations-output/
Share on other sites

such array as matrix

 

A1    A2    A3

 

B1    B2      B3

 

C1    C2      C3

 

 

must return the array containing all possible combinations of the matrix without repetitions

 

1. no pairs must return each array element seperately

2. 1 pair (2 elements) must return smth like

 

A1B1-A1B2-A1B3

A2B1-A2B2-A2B3

...

...

...

 

3. 3 (or size of array) elements must return

 

A1B1C1-A1B2C1-A1B3C1

A2B1C1-A2B2C1-A2B3C1

...

...

...

 

 

<?php
$array = array(
                        array('a1', 'a2', 'a3'),
                        array('b1', 'b2', 'b3'),
                        array('c1', 'c2', 'c3'),
                        array('x1', 'x2', 'x3')
                       );

function getCombinations($array, $string="", $i=0, $g=0, $data=array()) {
	$i++;
	$count=count($array);
	if ($g < $count) {
		for ($x=$g; $x < $count; $x++) {
			foreach ($array[$x] as $value) {
				$data[$i][]=$string.$value;
				$data=getCombinations($array, $string.$value."-", $i, $x+1, $data);
			}
		}	
	}
	return $data;
}

$myData=getCombinations($array);
echo "<pre>";
print_r($myData);
echo "</pre>";
?>

 

Be aware that if you add any more elements to that array, you are exponentially increasing the amount of results and could end up in a very long process.

Update: Reread how you wanted output paired. This is probably closer to what you wanted. You can then condense the arrays however you want.

 

<?php
$array = array(
                        array('a1', 'a2', 'a3'),
                        array('b1', 'b2', 'b3'),
                        array('c1', 'c2', 'c3'),
                        array('x1', 'x2', 'x3')
                       );

function getCombinations($array, $string="", $i=0, $g=0, $data=array(), $root=true, $key="") {
	$i++;
	$count=count($array);
	if ($g < $count) {
		for ($x=$g; $x < $count; $x++) {
			foreach ($array[$x] as $value) {
				if ($root) {
					$key=$value;
				}
				if (!isset($data[$key][$i])) {
					$data[$key][$i]="";
				}
				if (empty($data[$key][$i])) {
					$data[$key][$i]=$string.$value;
				}
				else {
					$data[$key][$i].="-".$string.$value;
				}
				$data=getCombinations($array, $string.$value, $i, $x+1, $data, false, $key);
			}
		}	
	}
	return $data;
}

$myData=getCombinations($array);
echo "<pre>";
print_r($myData);
echo "</pre>";
?>

Look at the output of your function

 

Array
(
    [a1] => Array
        (
            [1] => a1
            [2] => a1x3
            [3] => a1c3x3
            [4] => a1b3c3x3
        )

    [a2] => Array
        (
            [1] => a2
            [2] => a2x3
            [3] => a2c3x3
            [4] => a2b3c3x3
        )

    [a3] => Array
        (
            [1] => a3
            [2] => a3x3
            [3] => a3c3x3
            [4] => a3b3c3x3
        )

    [b1] => Array
        (
            [1] => b1
            [2] => b1x3
            [3] => b1c3x3
        )

    [b2] => Array
        (
            [1] => b2
            [2] => b2x3
            [3] => b2c3x3
        )

    [b3] => Array
        (
            [1] => b3
            [2] => b3x3
            [3] => b3c3x3
        )

    [c1] => Array
        (
            [1] => c1
            [2] => c1x3
        )

    [c2] => Array
        (
            [1] => c2
            [2] => c2x3
        )

    [c3] => Array
        (
            [1] => c3
            [2] => c3x3
        )

    [x1] => Array
        (
            [1] => x1
        )

    [x2] => Array
        (
            [1] => x2
        )

    [x3] => Array
        (
            [1] => x3
        )

)

 

There isn't an a1b1x1 anywhere. In any order.

xyph, also don't know if he wants to skip entries or not. :D By the way, I updated that second function.

 

Array
(
    [a1] => Array
        (
            [1] => a1
            [2] => a1b1-a1b2-a1b3-a1c1-a1c2-a1c3-a1x1-a1x2-a1x3
            [3] => a1b1c1-a1b1c2-a1b1c3-a1b1x1-a1b1x2-a1b1x3-a1b2c1-a1b2c2-a1b2c3-a1b2x1-a1b2x2-a1b2x3-a1b3c1-a1b3c2-a1b3c3-a1b3x1-a1b3x2-a1b3x3-a1c1x1-a1c1x2-a1c1x3-a1c2x1-a1c2x2-a1c2x3-a1c3x1-a1c3x2-a1c3x3
            [4] => a1b1c1x1-a1b1c1x2-a1b1c1x3-a1b1c2x1-a1b1c2x2-a1b1c2x3-a1b1c3x1-a1b1c3x2-a1b1c3x3-a1b2c1x1-a1b2c1x2-a1b2c1x3-a1b2c2x1-a1b2c2x2-a1b2c2x3-a1b2c3x1-a1b2c3x2-a1b2c3x3-a1b3c1x1-a1b3c1x2-a1b3c1x3-a1b3c2x1-a1b3c2x2-a1b3c2x3-a1b3c3x1-a1b3c3x2-a1b3c3x3
        )

    [a2] => Array
        (
            [1] => a2
            [2] => a2b1-a2b2-a2b3-a2c1-a2c2-a2c3-a2x1-a2x2-a2x3
            [3] => a2b1c1-a2b1c2-a2b1c3-a2b1x1-a2b1x2-a2b1x3-a2b2c1-a2b2c2-a2b2c3-a2b2x1-a2b2x2-a2b2x3-a2b3c1-a2b3c2-a2b3c3-a2b3x1-a2b3x2-a2b3x3-a2c1x1-a2c1x2-a2c1x3-a2c2x1-a2c2x2-a2c2x3-a2c3x1-a2c3x2-a2c3x3
            [4] => a2b1c1x1-a2b1c1x2-a2b1c1x3-a2b1c2x1-a2b1c2x2-a2b1c2x3-a2b1c3x1-a2b1c3x2-a2b1c3x3-a2b2c1x1-a2b2c1x2-a2b2c1x3-a2b2c2x1-a2b2c2x2-a2b2c2x3-a2b2c3x1-a2b2c3x2-a2b2c3x3-a2b3c1x1-a2b3c1x2-a2b3c1x3-a2b3c2x1-a2b3c2x2-a2b3c2x3-a2b3c3x1-a2b3c3x2-a2b3c3x3
        )

    [a3] => Array
        (
            [1] => a3
            [2] => a3b1-a3b2-a3b3-a3c1-a3c2-a3c3-a3x1-a3x2-a3x3
            [3] => a3b1c1-a3b1c2-a3b1c3-a3b1x1-a3b1x2-a3b1x3-a3b2c1-a3b2c2-a3b2c3-a3b2x1-a3b2x2-a3b2x3-a3b3c1-a3b3c2-a3b3c3-a3b3x1-a3b3x2-a3b3x3-a3c1x1-a3c1x2-a3c1x3-a3c2x1-a3c2x2-a3c2x3-a3c3x1-a3c3x2-a3c3x3
            [4] => a3b1c1x1-a3b1c1x2-a3b1c1x3-a3b1c2x1-a3b1c2x2-a3b1c2x3-a3b1c3x1-a3b1c3x2-a3b1c3x3-a3b2c1x1-a3b2c1x2-a3b2c1x3-a3b2c2x1-a3b2c2x2-a3b2c2x3-a3b2c3x1-a3b2c3x2-a3b2c3x3-a3b3c1x1-a3b3c1x2-a3b3c1x3-a3b3c2x1-a3b3c2x2-a3b3c2x3-a3b3c3x1-a3b3c3x2-a3b3c3x3
        )

    [b1] => Array
        (
            [1] => b1
            [2] => b1c1-b1c2-b1c3-b1x1-b1x2-b1x3
            [3] => b1c1x1-b1c1x2-b1c1x3-b1c2x1-b1c2x2-b1c2x3-b1c3x1-b1c3x2-b1c3x3
        )

    [b2] => Array
        (
            [1] => b2
            [2] => b2c1-b2c2-b2c3-b2x1-b2x2-b2x3
            [3] => b2c1x1-b2c1x2-b2c1x3-b2c2x1-b2c2x2-b2c2x3-b2c3x1-b2c3x2-b2c3x3
        )

    [b3] => Array
        (
            [1] => b3
            [2] => b3c1-b3c2-b3c3-b3x1-b3x2-b3x3
            [3] => b3c1x1-b3c1x2-b3c1x3-b3c2x1-b3c2x2-b3c2x3-b3c3x1-b3c3x2-b3c3x3
        )

    [c1] => Array
        (
            [1] => c1
            [2] => c1x1-c1x2-c1x3
        )

    [c2] => Array
        (
            [1] => c2
            [2] => c2x1-c2x2-c2x3
        )

    [c3] => Array
        (
            [1] => c3
            [2] => c3x1-c3x2-c3x3
        )

    [x1] => Array
        (
            [1] => x1
        )

    [x2] => Array
        (
            [1] => x2
        )

    [x3] => Array
        (
            [1] => x3
        )

)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.