change show for loop foreach


change show for loop foreach

我有

    $arr = array(1Atlanta, 2Chicago, 3Dallas, 4Detroit, 5Boston, 6Colorado, 7New York, 8San D, 9, 10 ...);
<table>
  <tr>
    foreach ($arr as $i => $value) {
        echo $value;
       if ($i % 10 == 0) { 
         echo "</tr> <tr>"
       }
    }
  </tr>
</table>

this show me

1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30

我该怎么做呢:

1 11 21 etc...
2 21 22 etc...
3 31 32 etc...
4 41 42 etc..
etc...

例如253 value?(1 - 253)我必须从数据库中获取值,所以我必须使用foreach,而不是for,并从上到下顺序放置,而不是从左到右谢谢你的帮助!

未经过测试,但应该接近您想要的内容。

$width = 10; // display 10 columns
$arr = array(...);
$rows = ceil(count($arr) / $width); // how many rows there'll be. e.g. 11 fields = 11 / 10 round up = 2 rows.
echo '<table>';
for ($i = 0; $i < $rows; $i++) { // row counter
   echo '<tr>';
   for ($j = 0; $j < $columns; $j++) { // column counter
      echo '<td>', $arr[($i * $width) + ($j * $width)], '</td>';
   }
   echo '</tr>';
}
echo '</table>';

很好,如果它必须是一个foreach循环,具有未知数量的元素,那么在进行任何输出之前,您必须将数据揉成基于列的格式。您可以将单独的行构建为字符串,但我将它们构建为数组,以防以后可能需要列格式。

$arr = array(...); // your source data
$width = 10;
$rows = ceil(count($array) / $width);
$sorted_data = array();
$cur_row = 0;
foreach($arr as $val) {
   $sorted_data[$cur_row][] = $val;
   $cur_row++;
   if ($cur_row >= $rows) {
      $cur_row = 0;
   }
}
echo '<table>';
foreach($sorted_data as $idx => $row) { // start a row
    echo '<tr>';
    foreach($row as $val) { // output the individual values in the row
        echo '<td>', $val, '</td>';
    }
    echo '</tr>';
}
echo '</table>';

试试下面的代码——可以根据自己的喜好随意调整:

<?php
$upper_bound = 253;
for ($i = 1; $i < 26; $i++){
  echo $i . " : ";
  for ($j = 0,$x = 1; $j < 10 ; $j++,$x++){
    $n =  $i * 10 + $x ;
    if ($n > $upper_bound){
      break;
    }
    echo $n ." ";
  }
  echo "<br />";
}
?>

    Output:
    1 : 11 12 13 14 15 16 17 18 19 20 
2 : 21 22 23 24 25 26 27 28 29 30 
3 : 31 32 33 34 35 36 37 38 39 40 
4 : 41 42 43 44 45 46 47 48 49 50 
5 : 51 52 53 54 55 56 57 58 59 60 
6 : 61 62 63 64 65 66 67 68 69 70 
7 : 71 72 73 74 75 76 77 78 79 80 
8 : 81 82 83 84 85 86 87 88 89 90 
9 : 91 92 93 94 95 96 97 98 99 100 
10 : 101 102 103 104 105 106 107 108 109 110 
11 : 111 112 113 114 115 116 117 118 119 120 
12 : 121 122 123 124 125 126 127 128 129 130 
13 : 131 132 133 134 135 136 137 138 139 140 
14 : 141 142 143 144 145 146 147 148 149 150 
15 : 151 152 153 154 155 156 157 158 159 160 
16 : 161 162 163 164 165 166 167 168 169 170 
17 : 171 172 173 174 175 176 177 178 179 180 
18 : 181 182 183 184 185 186 187 188 189 190 
19 : 191 192 193 194 195 196 197 198 199 200 
20 : 201 202 203 204 205 206 207 208 209 210 
21 : 211 212 213 214 215 216 217 218 219 220 
22 : 221 222 223 224 225 226 227 228 229 230 
23 : 231 232 233 234 235 236 237 238 239 240 
24 : 241 242 243 244 245 246 247 248 249 250 
25 : 251 252 253 

您期望的输出模式具有误导性。这是我能做的最好的了。

<?php
    $arr = range(1, 12);
?>
<table>
    <?php
        foreach ($arr as $value) {
            echo '<tr>';
            echo '<td>' . $value . '</td>';
            foreach (range(1, 9) as $i) {
                echo '<td>' . $i . $value . '</td>';
            }
            echo '</tr>';
        }
    ?>
</table>
输出:

1   11  21  31  41  51  61  71  81  91
2   12  22  32  42  52  62  72  82  92
3   13  23  33  43  53  63  73  83  93
4   14  24  34  44  54  64  74  84  94
5   15  25  35  45  55  65  75  85  95
6   16  26  36  46  56  66  76  86  96
7   17  27  37  47  57  67  77  87  97
8   18  28  38  48  58  68  78  88  98
9   19  29  39  49  59  69  79  89  99
10  110 210 310 410 510 610 710 810 910
11  111 211 311 411 511 611 711 811 911
12  112 212 312 412 512 612 712 812 912