Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array and keys combination

Status
Not open for further replies.

bledileka

Programmer
Nov 15, 2006
16
Hello !
Im got this array :

$numra=array(1,2,3,4,5,6....); //x numbers, max 90

Well, now i want to find all the possible unique combinations of this keys, i came up with something but i got problems on distinguing combinations.
Example : 1 - 4 - 3 - 5 - 6 is equal to 1 - 4 - 3 - 6 - 5. I need to show only one of this two.

This is the code (a real mess :D) :
Code:
$numra=array(1,2,3,4,5,6); //x numra mund te vendosen
$sas=count($numra);
for ($i=0;$i<=$sas;$i++) {
		$n2=$numra;
			$s2=count($n2);
				for ($j=$i+1;$j<=$s2;$j++) {
				if ($n2[$j]<>$numra[$i]) {
				$n3=$n2;
			    $s3=count($n3);
				for ($k=$i+2;$k<=$s3;$k++) {
				if (($n3[$k]<>$n2[$j]) & ($n3[$k]<>$numra[$i])) {	
				$n4=$n3;
			    $s4=count($n4);
				for ($l=$i+3;$l<=$s4;$l++) {
				if (($n4[$l]<>$n3[$k])&($n4[$l]<>$n2[$j])&($n4[$l]<>$numra[$i])) {
				$n5=$n4;
			    $s5=count($n5);
				for ($m=$i+4;$m<=$s5;$m++) {
				if (($n5[$m]<>$n4[$l])&($n5[$m]<>$n3[$k])&($n5[$m]<>$n2[$j])&($n5[$m]<>$numra[$i])) {
					if ((isset($n2[$j]))&(isset($n3[$k]))&(isset($n4[$l]))&(isset($n5[$m]))) {
					$xx=$numra[$i]+$n2[$j]+$n3[$k]+$n4[$l]+$n5[$m];
					$v[]=$xx;
					if (in_array($xx,$v)) {
					$tot++;
					echo $numra[$i]; echo " - ";echo $n2[$j];echo " - ";echo $n3[$k];echo " - ";echo $n4[$l];echo " - ";echo $n5[$m];echo "<br>";
					

}
}}}}}}}}
				
}}echo $tot." Kombinacione !";
Can anyone help me on this ???
 
Hi

Code:
$numra=array(1,2,3,4,5,6); //x numra mund te vendosen
$sas=count($numra);
for ($i=0;$i<=$sas;$i++) {
  $n2=$numra;
  $s2=count($n2);
  for ($j=$i+1;$j<=$s2;$j++) {
    if ($n2[$j]<>$numra[$i]) {
      $n3=$n2;
      $s3=count($n3);
      for ($k=$[red]j[/red]+[red]1[/red];$k<=$s3;$k++) {
        if (($n3[$k]<>$n2[$j]) &[red]&[/red] ($n3[$k]<>$numra[$i])) {
          $n4=$n3;
          $s4=count($n4);
          for ($l=$[red]k[/red]+[red]1[/red];$l<=$s4;$l++) {
            if (($n4[$l]<>$n3[$k])&[red]&[/red]($n4[$l]<>$n2[$j])&[red]&[/red]($n4[$l]<>$numra[$i])) {
              $n5=$n4;
              $s5=count($n5);
              for ($m=$[red]l[/red]+[red]1[/red];$m<=$s5;$m++) {
                if (($n5[$m]<>$n4[$l])&[red]&[/red]($n5[$m]<>$n3[$k])&[red]&[/red]($n5[$m]<>$n2[$j])&[red]&[/red]($n5[$m]<>$numra[$i])) {
                  if ((isset($n2[$j]))&[red]&[/red](isset($n3[$k]))&[red]&[/red](isset($n4[$l]))&[red]&[/red](isset($n5[$m]))) {
                    $xx=$numra[$i]+$n2[$j]+$n3[$k]+$n4[$l]+$n5[$m];
                    $v[]=$xx;
                    if (in_array($xx,$v)) {
                      $tot++;
                      echo "$numra[$i] - $n2[$j] - $n3[$k] - $n4[$l] - $n5[$m]\n";
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
echo $tot." Kombinacione !";
Please read the documentation more frequently to avoid mistakes like :
[ul]
[li]using [link
]Bitwise Operators[/url] instead of Logical Operators[/li]
[li]using a bunch of [tt]echo[/tt] statements instead of a single one[/li]
[/ul]
And please read some programming tips and tricks documentation too.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top