1

I am trying to generate all combinations from an array, and I have the array given below,

$myArray = array('A','B','C');

What is the simplest way to generate combinations like,

Array
(
    [0] => Array
        (
            [0] => A
            [1] => B
        )

    [1] => Array
        (
            [0] => B
            [1] => C
        )

    [2] => Array
        (
            [0] => A
            [1] => C
        )

    [3] => Array
        (
            [0] => A
            [1] => B
            [2] => C
        )
)

Thanks in advance

Anna
  • 973
  • 4
  • 13
  • 26
  • What you're attempting to generate would be similar to the power set (power array?) of your array. Some googling on that term may yield useful results. – CollinD Apr 12 '17 at 06:22
  • I think I found what you want just by searching a bit on google! -> https://gist.github.com/christophervalles/1066801 – Adarsh Sojitra Apr 12 '17 at 06:24
  • @Hmmm http://stackoverflow.com/questions/10222835/get-all-permutations-of-a-php-array it will return all combinations . In my case I dont need both AB and BA , both are same for me. – Anna Apr 12 '17 at 06:33
  • I think you can add your special condition in that solution provided ... Then it should return A, B, C too? Possibly this - http://stackoverflow.com/questions/10834393/php-how-to-get-all-possible-combinations-of-1d-array – Hmmm Apr 12 '17 at 06:34
  • No, if my array is [A, B, C]. Result array should be [AB, BC, AC, ABC], not [AB, AC, BA, BC, CA .........] – Anna Apr 12 '17 at 06:37
  • What if there are four elements [A, B, C, D] ? Always group of two or something else ? – Hmmm Apr 12 '17 at 06:42
  • If it is 4 element array , I need combinations of 2,3,4. – Anna Apr 12 '17 at 07:07
  • tatranskymedved, I tried many, but its not exactly the same I want. – Anna Apr 12 '17 at 07:08
  • So have you manged to solve your issue? – Hmmm Apr 13 '17 at 06:24

3 Answers3

0

EDIT The following code gives the result that you want (not just for array with 3 element like the code before the edited asnwer) but i doubt it is the best possible way, but since no one provided an answer you can use this until you find a better way

$myArray = array('A','B','C','D');

$result=array_combinations($myArray);

print_r($result);

function array_combinations($array){

    $result=[];
    for ($i=0;$i<count($array)-1;$i++) {
       $result=array_merge($result,combinations(array_slice($array,$i)));    
    }

    return $result;
}

function combinations($array){
    //get all the possible combinations no dublicates
    $combinations=[];

    $combinations[]=$array;
    for($i=1;$i<count($array);$i++){
        $tmp=$array;

        unset($tmp[$i]);
        $tmp=array_values($tmp);//fix the indexes after unset
        if(count($tmp)<2){
            break;
        }
        $combinations[]=$tmp;
    }


    return $combinations;
}

results

Array
(
    [0] => Array
        (
            [0] => A
            [1] => B
            [2] => C
            [3] => D
        )

    [1] => Array
        (
            [0] => A
            [1] => C
            [2] => D
        )

    [2] => Array
        (
            [0] => A
            [1] => B
            [2] => D
        )

    [3] => Array
        (
            [0] => A
            [1] => B
            [2] => C
        )

    [4] => Array
        (
            [0] => B
            [1] => C
            [2] => D
        )

    [5] => Array
        (
            [0] => B
            [1] => D
        )

    [6] => Array
        (
            [0] => B
            [1] => C
        )

    [7] => Array
        (
            [0] => C
            [1] => D
        )

)

you can find a demo here

knetsi
  • 1,601
  • 1
  • 16
  • 18
-1

Well, the simplest way is by just appending a new Subarray at the end:

$myarray = array()
$myarray[] = array('A', 'B');
$myarray[] = array('C', 'D');

You can use as many array dimensions as you want or need. But it becomes soon unreadable and/or unmanageable. Consider to use classes instead, to show other programmers what you meant with your data structure.

As you said combining arrays, you could also use array_merge it depends a bit on the specific situation, which option is better.

alpham8
  • 1,314
  • 2
  • 14
  • 32
-2
<?php
    echo "<table>";
    echo "<thead>";
    echo "<tr><th>Chemical One</th><th>Chemical two</th></tr>";
    echo "</thead>";
    echo "<tbody>";
    $chemical=array('c1','c2','c3','c4');
    $count_chem=count($chemical);
    for ($i=0; $i <= $count_chem; $i++) { 

            for ($j=$i+1; $j < $count_chem; $j++) { 
                echo "<tr>";
                echo "<td>".$chemical[$i]."</td>";
                echo "<td>".$chemical[$j]."</td>";

                echo "<tr>";
            }
    }
    echo "</tbody></table>";
?>

the question is similar Click How to compare one element from same table to another element to already asked.

ThataL
  • 165
  • 3
  • 12