0

I have an array() contains string F1, F2, F3, F4.........

<?php
  $facts= array("F1", "F2", "F3", "F4);
?>

How can I generate combinations two elements of that array. The output can be like that:
F1.F2
F1.F3
F1.F4
F2.F3
F2.F4
F3.F4

Please help me

Hàm Louis
  • 41
  • 7

1 Answers1

1

Try this:

Solution

$facts= array("F1", "F2", "F3", "F4");

$new_array = array();
foreach($facts as $key => $val){
    foreach($facts as $key2 => $val2){
        if($key2 <= $key) continue;
        $new_array[] = $val . '.' . $val2;
    }
}

print_r($new_array);

Output

Array
(
    [0] => F1.F2
    [1] => F1.F3
    [2] => F1.F4
    [3] => F2.F3
    [4] => F2.F4
    [5] => F3.F4
)
Bluetree
  • 1,324
  • 2
  • 8
  • 25
  • Thanks, If I want to generate combinations of 1 to n elements of array() has n value. how can I do? The output should be : "F1","F2","F3","F1.F2","F1.F3","F2.F3","F1.F2.F3" – Hàm Louis Dec 04 '19 at 03:48
  • @HàmLouis I think you should create another question for that. – Bluetree Dec 04 '19 at 03:50