1

I have this system with configuration items and combinations made of these configuration items. What I try to achieve is selecting the combinations which are not yet made.

System example:

Configuration apple id's: 1, 2, 3

Configuration peer id's: 4,5,6,7,8

Configuration mango id's: 9,10,11,12,13

The combination is always in the order apple, peer and then mango.

For now I try to accomplish to make all possible combinations of the available configuration items.

Thus: Apple id: 1 Peer id: 4 Mango id: 9

Another combination: Apple id: 1 Peer id: 4 Mango id: 10

Etcetera.

I wonder how to make all possible combinations from the apple, peer and then mango id's.

Hopefully I describe it clearly. I have no idea how to accomplish this but my first guess is:

$apples = array();
query select appleid FROM apples
while($apples_sql = mysqli_fetch_assoc($result)) {
{
 $apples[] =  $row["appleid"];
}
$peers = array();
query select peerid FROM peers
while($peers_sql = mysqli_fetch_assoc($result)) {
{
 $peers[] =  $row["peerid"];
}
$mango = array();
query select mangoid FROM mango
while($mango_sql = mysqli_fetch_assoc($result)) {
{
 $mango[] =  $row["mangoid"];
}

Then I have all apples, peers and mango ID's in an array. But then I have no idea how to make the combinations.

Hopefully someone can help me out. Any help is much appreciated, thanks in advance.

2 Answers2

1

have look on below function:

/**
 * Generate all the possible combinations among a set of nested arrays.
 *
 * @param array $data  The entrypoint array container.
 * @param array $all   The final container (used internally).
 * @param array $group The sub container (used internally).
 * @param mixed $val   The value to append (used internally).
 * @param int   $i     The key index (used internally).
 */
function generate_combinations(array $data, array &$all = array(), array $group = array(), $value = null, $i = 0)
{
    $keys = array_keys($data);
    if (isset($value) === true) {
        array_push($group, $value);
    }

    if ($i >= count($data)) {
        array_push($all, $group);
    } else {
        $currentKey     = $keys[$i];
        $currentElement = $data[$currentKey];
        foreach ($currentElement as $val) {
            generate_combinations($data, $all, $group, $val, $i + 1);
        }
    }

    return $all;
}
$apple = array(1,2,3);
$peer = array(4,5,6,7,8);
$mengo = array(9,10,11,12,13);
$data = array(
    $apple,
    $peer,
    $mengo,
);

$combos = generate_combinations($data);
print_r($combos);

$combos variable will have all valid combination in artray format.

Chetan Ameta
  • 7,696
  • 3
  • 29
  • 44
1

suppose all three array's with own id's use nested for loop for generate combination

for($a=0;$a<sizeof($apple);$a++){
  for($p=0;$p<sizeof($peers);$p++){
     for($m=0;$m<sizeof($mango);$m++){
   $combination=$apple[$a].",".$peers[$p].",".$mango[$m];
     echo $combination. "<br>";
}
}
}

i hope this will help you

Shailesh Singh
  • 317
  • 1
  • 11