0

I am working on the dynamic reports here i want some combinations.Consider i have a array('1,'2','3','4') i want combinations as 1,2 1,3 1,4 but not like 1,2,3

Tried almost everything but not the solution.

Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
varad mayee
  • 619
  • 7
  • 19

1 Answers1

6

This will perfectly fine for the case when you are trying to create combinations of 2

Try this code snippet here

<?php

ini_set('display_errors', 1);
$array = array('1', '2', '3', '4');
$combinations = array();
foreach ($array as $key1 => $value1)
{
    foreach ($array as $key2 => $value2)
    {
        if ($value1 != $value2)
        {
            $combinations[] = $value1 . "," . $value2;
        }
    }
}
print_r($combinations);

Output:

Array
(
    [0] => 1,2
    [1] => 1,3
    [2] => 1,4
    [3] => 2,1
    [4] => 2,3
    [5] => 2,4
    [6] => 3,1
    [7] => 3,2
    [8] => 3,4
    [9] => 4,1
    [10] => 4,2
    [11] => 4,3
)
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42