Php 更改循环foreach的显示

Php 更改循环foreach的显示,php,foreach,Php,Foreach,我有 等 我怎样才能做到这一点: 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 例如253值?[1-253] 我必须从数据库中获取值,所以我必须使用foreach,而不是for,并从上到下按顺序放置,但不能从左到右 谢谢你的帮助 您所期望的输出模式具有误导性。这是我能做的最好的了 1 11 21 etc... 2 21 22 etc... 3 31 32 etc... 4 41

我有

我怎样才能做到这一点:

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

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

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

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

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
$width=10;//显示10列
$arr=数组(…);
$rows=ceil(计数($arr)/$WITH);//一共有多少排。e、 g.11个字段=11/10取整=2行。
回声';
对于($i=0;$i<$rows;$i++){//行计数器
回声';
对于($j=0;$j<$columns;$j++){//列计数器
回声“”,$arr[($i*$width)+($j*$width)],“”;
}
回声';
}
回声';

很好,如果它必须是一个foreach循环,并且元素数量未知,那么在进行任何类型的输出之前,必须将数据转换为基于列的格式。您可以将单个行构建为字符串,但我正在将它们构建到一个数组中,以防以后可能需要使用列格式

$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>';
$arr=array(…);//您的源数据
$width=10;
$rows=ceil(计数($array)/$WITH);
$sorted_data=array();
$cur_row=0;
外汇($arr作为$val){
$sorted_data[$cur_row][]=$val;
$cur_row++;
如果($cur_row>=$rows){
$cur_row=0;
}
}
回声';
foreach($idx=>$row)的排序数据{//开始一行
回声';
foreach($row as$val){//输出行中的各个值
回音“”,$val“”;
}
回声';
}
回声';
试试这段代码——可以根据自己的喜好随意调整:

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

输出:
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 

?在
echo'
我必须使用foreach时,您还缺少一个分号。我将为数据库获取数据mysql@Remind例如你应该一开始就这样说。你不会去找医生要绷带,然后在出门时告诉他你头上有斧头伤……编辑:这有一列——从上到下,但我有12列。例如,表12x12,但不是1x244,因为输出将遵循您的模式。你希望它是什么?请更新您的答案。我从上到下依次获取数据库、美国城市列表的值,但不是从左到右。谢谢,但我必须使用loop foreach。我从数据库中获取值,这是美国城市的列表。我希望按从上到下的顺序放置,但不是从左到右
$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>';
$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