tags:

views:

89

answers:

3

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.

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
+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
+1 for answering the question helpfully (i.e. with constructive advice).
BoltClock
You should consider his answer accepted by checking his answer as the accepted solution so that this question is solved. :)
YouBook
I don't think this meets the requirements. This would produce 0000, 0001...9999.
mkoistinen
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