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

0

1 Answer 1

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
)
Sign up to request clarification or add additional context in comments.

2 Comments

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àmLouis I think you should create another question for that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.