首頁  >  文章  >  後端開發  >  一个关于数组循环的有关问题

一个关于数组循环的有关问题

WBOY
WBOY原創
2016-06-13 12:02:32912瀏覽

一个关于数组循环的问题
现有一数组如下

$array = array ('A' => 'a', 'B' => 'b', 'C' => 'c', 'D' => 'd', 'E' => 'e', 'F' => 'f', 'G' => 'g', 'H' => 'h', 'I' => 'i', 'J' => 'j', 'K' => 'k', 'L' => 'l', 'M' => 'm', 'N' => 'n', 'O' => 'o', 'P' => 'p', 'Q' => 'q', 'R' => 'r', 'S' => 's', 'T' => 't', 'U' => 'u', 'V' => 'v', 'W' => 'w', 'X' => 'x', 'Y' => 'y', 'Z' => 'z' );<br />


需要循环数组实现以下效果
<table><br />		<tbody><br />			<tr><br />				<td>A</td><br />				<td>B</td><br />				<td>C</td><br />				<td>D</td><br />				<td>E</td><br />				<td>F</td><br />			</tr><br />			<tr><br />				<td>a</td><br />				<td>b</td><br />				<td>c</td><br />				<td>d</td><br />				<td>e</td><br />				<td>f</td><br />			</tr><br />			<tr><br />				<td>G</td><br />				<td>H</td><br />				<td>I</td><br />				<td>J</td><br />				<td>K</td><br />				<td>L</td><br />			</tr><br />			<br />			<tr><br />				<td>g</td><br />				<td>h</td><br />				<td>i</td><br />				<td>j</td><br />				<td>k</td><br />				<td>l</td><br />			</tr><br />			<tr><br />				<td>M</td><br />				<td>N</td><br />				<td>O</td><br />				<td>P</td><br />				<td>Q</td><br />				<td>R</td><br />			</tr><br />			<tr><br />				<td>m</td><br />				<td>n</td><br />				<td>o</td><br />				<td>p</td><br />				<td>q</td><br />				<td>r</td><br />			</tr>			<br />			<tr><br />				<td>S</td><br />				<td>T</td><br />				<td>U</td><br />				<td>V</td><br />				<td>W</td><br />				<td>X</td><br />			</tr><br />			<tr><br />				<td>s</td><br />				<td>t</td><br />				<td>u</td><br />				<td>v</td><br />				<td>w</td><br />				<td>x</td><br />			</tr><br />			<tr><br />				<td>Y</td><br />				<td>Z</td><br />				<td></td><br />				<td></td><br />				<td></td><br />				<td></td><br />			</tr>	<br />			<tr><br />				<td>y</td><br />				<td>z</td><br />				<td></td><br />				<td></td><br />				<td></td><br />				<td></td><br />			</tr><br />		</tbody><br />	</table>


请问各位该如何实现呢
------解决方案--------------------
$array = array ('A' => 'a', 'B' => 'b', 'C' => 'c', 'D' => 'd', 'E' => 'e', 'F' => 'f', 'G' => 'g', 'H' => 'h', 'I' => 'i', 'J' => 'j', 'K' => 'k', 'L' => 'l', 'M' => 'm', 'N' => 'n', 'O' => 'o', 'P' => 'p', 'Q' => 'q', 'R' => 'r', 'S' => 's', 'T' => 't', 'U' => 'u', 'V' => 'v', 'W' => 'w', 'X' => 'x', 'Y' => 'y', 'Z' => 'z' );<br />
for($i=0; $i echo '', PHP_EOL;
$t1 = array_keys(array_slice($array, $i, 6)) + array_fill(0, 6, '');
$t2 = array_values(array_slice($array, $i, 6)) + array_fill(0, 6, '');
foreach($t1 as $v) {
echo " $v", PHP_EOL;
}
echo '', PHP_EOL;

echo '', PHP_EOL;
foreach($t2 as $v) {
echo " $v", PHP_EOL;
}
echo '', PHP_EOL;
}

<tr>
<br>  <td>A</td>
<br>  <td>B</td>
<br>  <td>C</td>
<br>  <td>D</td>
<br>  <td>E</td>
<br>  <td>F</td>
<br>
</tr><br><tr>
<br>  <td>a</td>
<br>  <td>b</td>
<br>  <td>c</td>
<br>  <td>d</td>
<br>  <td>e</td>
<br>  <td>f</td>
<br>
</tr><br><tr>
<br>  <td>G</td>
<br>  <td>H</td>
<br>  <td>I</td>
<br>  <td>J</td>
<br>  <td>K</td>
<br>  <td>L</td>
<br>
</tr><br><tr>
<br>  <td>g</td>
<div class="clear">
                 
              
              
        
            </div>
</tr>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn