一个关于数组循环的问题
程序员文章站
2022-04-07 16:50:26
...
现有一数组如下
需要循环数组实现以下效果
请问各位该如何实现呢
' . PHP_EOL; foreach($newArray as $value){ $tr = '
'. PHP_EOL; echo $html; ?>
';for($i=0; $i'; echo '
';
$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 | B | C | D | E | F |
a | b | c | d | e | f |
G | H | I | J | K | L |
g | h | i | j | k | l |
M | N | O | P | Q | R |
m | n | o | p | q | r |
S | T | U | V | W | X |
s | t | u | v | w | x |
Y | Z | ||||
y | z |
请问各位该如何实现呢
回复讨论(解决方案)
$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 '
A B C D E F a b c d e f G H I J K L g h i j k l M N O P Q R m n o p q r S T U V W X s t u v w x Y Z y z
'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' );; $keys = array_chunk(array_keys($array),6); $values = array_chunk(array_values($array),6); $newArray = array(); foreach($keys as $key=>$item){ $newArray[] = $item; $newArray[] = $values[$key]; } $html = '
A | B | C | D | E | F |
a | b | c | d | e | f |
G | H | I | J | K | L |
g | h | i | j | k | l |
M | N | O | P | Q | R |
m | n | o | p | q | r |
S | T | U | V | W | X |
s | t | u | v | w | x |
Y | Z | ||||
y | z |
foreach(array_chunk(array_keys($array), 6) as $v){ $len = count($v); if($len \n"; echo "".implode(" \n",$v)." \n"; echo "\n"; $vl = array_map('strtolower', $v); echo "\n"; echo " \n"; }".implode(" \n",$vl)." \n"; echo "
感谢几位大神的回复 小弟收益匪浅, 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) $v\n"; echo " \n"; }
一层循环便可。
$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 '
'.implode(' | ',array_pad(array_keys(array_slice($arr, $i*6, 6)),6,'')).' | '; echo ''; echo '
'.implode(' | ',array_pad(array_slice($arr, $i*6, 6),6,'')).' | '; echo '