-2

I searched the site but I could not find what I need and I do not know how to do this code. I need a script to make all the combinations in an array

For example i have this array:

$players = ('1', '2', '3', '4', '5');

And i need this output

1 - 2
1 - 3
1 - 4
1 - 5
2 - 3
2 - 4
2 - 5
3 - 4
3 - 5
4 - 5

Thanks in advance

6 Answers6

2
$players = array('1','2','3','4','5');

while(count($players) != 0){
    $currentPlayer = array_shift($players);
    foreach($players as $player){
        echo $currentPlayer.' - '.$player.'<br/>';
    }
}

Edit: My code works, even if the playernumbers are nonconsecutive. The $players array can look like this $players = ('1','5','209','42'); and still print out the desired output.

kscherrer
  • 5,486
  • 2
  • 19
  • 59
0

simple way :--

function combinations($arr, $n)
{
    $res = array();

    foreach ($arr[$n] as $item)
    {
        if ($n==count($arr)-1)
            $res[]=$item;
        else
        {
            $combs = combinations($arr,$n+1);

            foreach ($combs as $comb)
            {
                $res[] = "$item $comb";
            }
        }
    }
    return $res;
}

$words = array(array('A','B'),array('C','D'), array('E','F'));

$combos = combinations($words,1);  
echo '<pre>';
print_r($combos);
echo '</pre>';
?>

output:--

Array
(
    [0] => C E
    [1] => C F
    [2] => D E
    [3] => D F
)
0
  $players = array('1', '2', '3', '4', '5');

          for($i=0;$i<sizeof($players)-1;$i++){
               for($j=$i;$j<sizeof($players);$j++){
                   if( $players[$i]!= $players[$j]){
                          echo  '<br/>';
                       echo $players[$i].' - '.$players[$j];
                   }
              }
          }

Output -

1 - 2
1 - 3
1 - 4
1 - 5
2 - 3
2 - 4
2 - 5
3 - 4
3 - 5
4 - 5
Krishan Kumar
  • 201
  • 4
  • 8
-1

try this :

<?php
  $arr = array('1','2', '3', '4', '5');

  for($i;$i<=count($arr);$i++){
    for($y=$i+1;$y<count($arr); $y++){
       echo ($i+1).'-'.$arr[$y].PHP_EOL;
    }
  }
?>
Fky
  • 2,133
  • 1
  • 15
  • 23
-1
    $players = array('1', '3', '5', '7', '9');

    for($i=0; $i<count($players); $i++) {
        for($j=$i+1; $j<count($players); $j++){
            echo ($players[$i]).' - '.$players[$j].PHP_EOL;
        }
    }

this sould work for any values in the array

MaK
  • 596
  • 4
  • 23
-3

One foreach loop and a for loop that starts at foreach value +1.

$players =array ('1', '2', '3', '4', '5');

Foreach($players as $player){

    For($i=$player+1; $i<=count($players); $i++){
        Echo $player ."-" .$i."\n";
    }
}

https://3v4l.org/Bunia

Andreas
  • 23,610
  • 6
  • 30
  • 62
  • @MaK that is not part of OPs scope. Leave the scope and requirements to OP. – Andreas Nov 23 '17 at 12:18
  • @MaK why do you add requirements to the question that the OP has not asked for? Can you answer my question? – Andreas Nov 24 '17 at 04:32
  • The question state that the OP want all combinations of "an array" so any given array, not that specific array; So how am i adding requirements ? Plus you're not even working with the array values in your inner for loop but only an index based on the array count.. ;/ Your answer is just all tha way wrong PS: I'm not gonna answer any following comments :) – MaK Nov 24 '17 at 09:07
  • @MaK the question says *i have this array:*. Yes I use the counter index, that is how a for loop works. Also your method is not all that great either. If you think "an array" can be read as any possible array that is not part of requirements, question, scope or example. Then try this in your code please `[1,2,[3],4,5]`. This is also **an array** but just as your it's not part of the requirements, scope, question or example. – Andreas Nov 24 '17 at 09:17