note 69525 modified in control-structures.for by danbrown
| From: | danbrown@php.net | Date: | Mon, 07 Dec 2009 16:02:38 +0000 |
| Subject: | note 69525 modified in control-structures.for by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-163646@lists.php.net to get a copy of this message | ||
On the combination problem again...
It seems to me like it would make more sense to go through systematically. That would take nested
for loops, where each number was put through all of it's potentials sequentially.
The following would give you all of the potential combinations of a four-digit decimal combination,
printed in a comma delimited format:
<?php
for($a=0;$a<10;$a++){
for($b=0;$b<10;$b++){
for($c=0;$c<10;$c++){
for($d=0;$d<10;$d++){
echo $a.$b.$c.$d.", ";
}
}
}
}
?>
Of course, if you know that the numbers you had used were in a smaller subset, you could just plunk
your possible numbers into arrays $a, $b, $c, and $d and then do nested foreach loops as above.
- Elizabeth
--was--
On the combination problem again...
It seems to me like it would make more sense to go through systematically. That would take nested
for loops, where each number was put through all of it's potentials sequentially.
The following would give you all of the potential combinations of a four-digit decimal combination,
printed in a comma delimited format:
for($a=0;$a<10;$a++){
for($b=0;$b<10;$b++){
for($c=0;$c<10;$c++){
for($d=0;$d<10;$d++){
echo $a.$b.$c.$d.", ";
}
}
}
}
Of course, if you know that the numbers you had used were in a smaller subset, you could just plunk
your possible numbers into arrays $a, $b, $c, and $d and then do nested foreach loops as above.
- Elizabeth
http://php.net/manual/en/control-structures.for.php