How would I print all values 0...9999 using an array of $array = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
? No clue, please help.
views:
89answers:
3
A:
<?php
$array = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
foreach ($array as $a) {
foreach ($array as $b) {
foreach ($array as $c) {
foreach ($array as $d) {
if ($a) echo $a;
if ($a || $b) echo $b;
if ($a || $b || $c) echo $c;
echo $d. "<br />\n";
}
}
}
}
aularon
2010-09-06 23:38:25
+3
A:
This would seem like an homework assignment, so some thinking of your own should apply. Example below is specifically strict to the assignment, it is in no way the best solution for just displaying all number between 0 and 9999.
$array = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
foreach($array as $value_1)
foreach($array as $value_2)
foreach($array as $value_3)
foreach($array as $value_4)
echo $value_1.$value_2.$value_3.$value_4.PHP_EOL;
ontrack
2010-09-06 23:41:04
+1 for answering the question helpfully (i.e. with constructive advice).
BoltClock
2010-09-06 23:47:36
You should consider his answer accepted by checking his answer as the accepted solution so that this question is solved. :)
YouBook
2010-09-07 03:16:31
I don't think this meets the requirements. This would produce 0000, 0001...9999.
mkoistinen
2010-09-07 11:34:50
A:
$array = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
foreach ($array as $i) {
foreach ($array as $j) {
foreach ($array as $k) {
foreach ($array as $l) {
print $i*1000+$j*100+$k*10+$l."\n";
}
}
}
}
Smells like a homework assignment ;)
mkoistinen
2010-09-06 23:42:32