既存の配列は次のとおりです
$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' );
<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>
$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 " ', PHP_EOL;}$v ", PHP_EOL; } echo '
<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>
$item){ $newArray[] = $item; $newArray[] = $values[$key]; } $html = '
$text | ". PHP_EOL; } $tr .= '
<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>
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"; }
複数の専門家からの回答に感謝します。モデレータの 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 " \n"; }$v \n"; echo "
ループは 1 層だけです。
りー