Jump to content

Combinations Problem


Cruisecar

Recommended Posts

Firstly thanks for reading!

 

I Have an Array($options)  -

 

[9] = [12], [18], [20]

[12] = [3], [6]

[18] = [19], [13]

 

Basically I want something that can give me all of the possible combinations. within a loop -

 

1 = 12,3,19

2 = 12,3,13

e.t.c.

 

while(something){

$x++;

$insert = "insert into table(`id`,`option`,`value`) values('x','x','x');

$insert = "insert into table(`id`,`option`,`value`) values('x','x','x');

$insert = "insert into table(`id`,`option`,`value`) values('x','x','x');

}

 

So the first combination would be entered as 3 rows into the database as so:

 

$insert = "insert into table(`id`,`option`,`value`) values('100','9','12');

$insert = "insert into table(`id`,`option`,`value`) values('100','12','3');

$insert = "insert into table(`id`,`option`,`value`) values('100,'18','19');

 

Next lot:

 

$insert = "insert into table(`id`,`option`,`value`) values('101','9','12');

$insert = "insert into table(`id`,`option`,`value`) values('101','12','3');

$insert = "insert into table(`id`,`option`,`value`) values('101','18','13');

 

Hope somebody can help me. It needs to be something that works no matter how many values there are within the array.  :)

 

Link to comment
Share on other sites

try

<?php
$options = array(
9 => array(12, 18, 20),
12 => array(3, 6),
18 => array(19, 13)
);
$total_comb = 1;
$keys = array();
$key_count = array();
$out = array();
foreach ($options as $k => $v){
$keys[] = $k;
$total_comb *= count($v);
$key_count[$k] = count($v);
}
$keys = array_reverse($keys);
for ($x = 0; $x < $total_comb; $x++){
$i = $x;
$tmp = array();
foreach ($keys as $k){
$a = $i % $key_count[$k];
$i = (int) ($i / $key_count[$k]);
$tmp[] = array('option' => $k,'value' => $options[$k][$a]);
}
$out[] = array_reverse($tmp);
}
print_r($out);
?>

Link to comment
Share on other sites

try

<?php
$a = array ('a1', 'a2', 'a3', 'a4');
$b = array ('b1', 'b2');
$c = array ('c1', 'c2', 'c3');

for ($x=0, $ka=count($a); $x < $ka; $x++)
{
for ($y=0, $kb=count($b); $y < $kb; $y++)
{
    for ($z=0, $kc=count($c); $z < $kc; $z++)
    {
    	echo "$a[$x] $b[$y] $c[$z] <br/>";
    }
}
} 
?>

gives-->
a1 b1 c1 
a1 b1 c2 
a1 b1 c3 
a1 b2 c1 
a1 b2 c2 
a1 b2 c3 
a2 b1 c1 
a2 b1 c2 
a2 b1 c3 
a2 b2 c1 
a2 b2 c2 
a2 b2 c3 
a3 b1 c1 
a3 b1 c2 
a3 b1 c3 
a3 b2 c1 
a3 b2 c2 
a3 b2 c3 
a4 b1 c1 
a4 b1 c2 
a4 b1 c3 
a4 b2 c1 
a4 b2 c2 
a4 b2 c3 

Link to comment
Share on other sites

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.