使用FOR EACH和Array在PHP中获得0到9999?

时间:2010-09-06 23:20:49

标签: php arrays loops

如何使用$array = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);数组打印所有值0 ... 9999?不知道,请帮忙。

3 个答案:

答案 0 :(得分:4)

这看起来像是一项家庭作业,所以有些想法应该适用。以下示例具体严格的赋值,它绝不是仅显示0到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;

答案 1 :(得分:0)

<?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";
      }
    }
  }
}

答案 2 :(得分:0)

$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";
         }
      }
   }
}

闻起来像家庭作业;)