一个关于数组循环的问题

WBOY
Freigeben: 2016-06-23 13:53:44
Original
979 Leute haben es durchsucht

现有一数组如下

$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' );
Nach dem Login kopieren


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


请问各位该如何实现呢


回复讨论(解决方案)

$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' );for($i=0; $i', 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;}
Nach dem Login kopieren
<tr>  <td>A</td>  <td>B</td>  <td>C</td>  <td>D</td>  <td>E</td>  <td>F</td></tr><tr>  <td>a</td>  <td>b</td>  <td>c</td>  <td>d</td>  <td>e</td>  <td>f</td></tr><tr>  <td>G</td>  <td>H</td>  <td>I</td>  <td>J</td>  <td>K</td>  <td>L</td></tr><tr>  <td>g</td>  <td>h</td>  <td>i</td>  <td>j</td>  <td>k</td>  <td>l</td></tr><tr>  <td>M</td>  <td>N</td>  <td>O</td>  <td>P</td>  <td>Q</td>  <td>R</td></tr><tr>  <td>m</td>  <td>n</td>  <td>o</td>  <td>p</td>  <td>q</td>  <td>r</td></tr><tr>  <td>S</td>  <td>T</td>  <td>U</td>  <td>V</td>  <td>W</td>  <td>X</td></tr><tr>  <td>s</td>  <td>t</td>  <td>u</td>  <td>v</td>  <td>w</td>  <td>x</td></tr><tr>  <td>Y</td>  <td>Z</td>  <td></td>  <td></td>  <td></td>  <td></td></tr><tr>  <td>y</td>  <td>z</td>  <td></td>  <td></td>  <td></td>  <td></td></tr>
Nach dem Login kopieren

$item){    $newArray[] = $item;    $newArray[] = $values[$key];  }  $html = '' . PHP_EOL;  foreach($newArray as $value){    $tr = ''. PHP_EOL;    for($i=0;$i<6;$i++){      $text = empty($value[$i])?'':$value[$i];      $tr .=  "". PHP_EOL;    }   $tr .= ''. PHP_EOL;   $html .= $tr;  }  $html .= '
$text
'. PHP_EOL; echo $html; ?>
Nach dem Login kopieren

<table><tbody><tr><td>A</td><td>B</td><td>C</td><td>D</td><td>E</td><td>F</td></tr><tr><td>a</td><td>b</td><td>c</td><td>d</td><td>e</td><td>f</td></tr><tr><td>G</td><td>H</td><td>I</td><td>J</td><td>K</td><td>L</td></tr><tr><td>g</td><td>h</td><td>i</td><td>j</td><td>k</td><td>l</td></tr><tr><td>M</td><td>N</td><td>O</td><td>P</td><td>Q</td><td>R</td></tr><tr><td>m</td><td>n</td><td>o</td><td>p</td><td>q</td><td>r</td></tr><tr><td>S</td><td>T</td><td>U</td><td>V</td><td>W</td><td>X</td></tr><tr><td>s</td><td>t</td><td>u</td><td>v</td><td>w</td><td>x</td></tr><tr><td>Y</td><td>Z</td><td></td><td></td><td></td><td></td></tr><tr><td>y</td><td>z</td><td></td><td></td><td></td><td></td></tr></tbody></table> 
Nach dem Login kopieren

foreach(array_chunk(array_keys($array), 6) as $v){     $len = count($v);	 if($len < 6){	     $v = array_merge($v, array_fill(0, 6-$len, ''));	 }     echo "<tr>\n";	 echo "<td>".implode("</td>\n<td>",$v)."</td>\n";	 echo "</tr>\n";	 	 $vl = array_map('strtolower', $v);	 echo "<tr>\n";	 echo "<td>".implode("</td>\n<td>",$vl)."</td>\n";	 echo "</tr>\n";	 }
Nach dem Login kopieren

感谢几位大神的回复 小弟收益匪浅, jordan102 版主可能没理解这个问题的本质其实是想隔行显示标题和内容 ,而不是把大写转为小写的问题 ,可能是我给的数组误导了你,不过如果针对我给数组,你的思路也很值得学习, 再次感谢几位

楼主新人啊!加油!过来凑个热闹!

php 有丰富的数组函数,这里再提供一种写法

$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' );$a = array_map(null, array_chunk(array_keys($array), 6), array_chunk($array, 6));foreach($a as $rs)  foreach($rs as $r) {    echo "\n";    if(count($r) < 6) $r = array_pad($r, 6, '');    foreach($r as $v) echo "$v\n";    echo "\n";  }
Nach dem Login kopieren

一层循环便可。

$arr = 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' );$page = (int)((count($arr)-1)/6)+1;echo '<table border="1"><tbody>';for($i=0; $i<$page; $i++){    echo '<tr>';    echo '<td>'.implode('</td><td>',array_pad(array_keys(array_slice($arr, $i*6, 6)),6,'')).'</td>';    echo '</tr>';    echo '<tr>';    echo '<td>'.implode('</td><td>',array_pad(array_slice($arr, $i*6, 6),6,'')).'</td>';    echo '</tr>';}echo '</tbody></table>';
Nach dem Login kopieren


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage