【问题标题】:change show for loop foreach更改显示 for 循环 foreach
【发布时间】:2011-08-03 18:31:50
【问题描述】:

我有

    $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>

这个给我看

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 值? [1-253] 我必须从数据库中获取值,所以我必须使用foreach,而不是for,并且从上到下顺序放置,而不是从左到右 感谢您的帮助!

【问题讨论】:

    标签: php foreach


    【解决方案1】:

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

    $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>';
    

    【讨论】:

    • ? codepad.org/zpA4V5QFecho '&lt;table&gt;' 上还缺少分号
    • 我必须使用 foreach。我将获取数据库 mysql 的数据
    • @Remind:你应该从一开始就这么说。你不会去医生那里买创可贴,然后在你走出门时告诉他你的头部被斧头伤了......
    • 进行编辑:这有一列 - 从上到下,但我有例如 12 列。例如表 12x12,但不是 1x244
    【解决方案2】:

    试试这个代码 - 随意调整它到你的喜好:

    <?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 
    

    【讨论】:

    • 谢谢,但我必须使用循环 foreach。我从数据库中获取值,这是美国城市的列表。我想从上到下依次放置,而不是从左到右
    【解决方案3】:

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

    <?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
    

    【讨论】:

    • 因为这就是输出将遵循您的模式。你希望它变成什么?请更新您的答案。
    • 我获取数据库的值,美国城市列表从上到下依次排列,但不是从左到右
    猜你喜欢
    • 1970-01-01
    • 2020-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多