PHP如何在5x5表中对数组的输出进行排序?
我对PHP非常陌生。 我正在尝试将游戏(名称)放入一个5行5列的5x5表中。 但是我想根据游戏的输出数量生成表格,而不是预设的html表格。 我让它工作,它显示的游戏,但不是在一个表。 你能帮我解释一下吗?这样我也能理解。 谢谢,已经有了PHP如何在5x5表中对数组的输出进行排序?,php,css,html,Php,Css,Html,我对PHP非常陌生。 我正在尝试将游戏(名称)放入一个5行5列的5x5表中。 但是我想根据游戏的输出数量生成表格,而不是预设的html表格。 我让它工作,它显示的游戏,但不是在一个表。 你能帮我解释一下吗?这样我也能理解。 谢谢,已经有了 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Merijn</title>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Merijn</title>
</head>
<body>
<?php
$games = array(
"battlefield 1",
"battlefield 2",
"Out of the Park Baseball 17",
"Overwatch",
"Stephen's Sausage Roll",
"Dark Souls III",
"Kentucky Route Zero - Act IV NEW",
"Stardew Valley",
"Ori and the Blind Forest",
"XCOM 2",
"World of Warcraft: Legion",
"Steins;Gate ",
"The Witness",
"Inside",
"Hex: Shards of Fate",
"Forza Horizon 3 ",
"Rise of the Tomb Raider",
"Total War: WARHAMMER",
"Chime Sharp",
"Pony Island",
"F1 2016",
"Day of the Tentacle Remastered",
"Tales from the Borderlands",
"DOOM",
"Hearthstone");
for($i =0; $i <=25; $i++){
echo '<table rows=5 cols=5>';
echo "<td>" . $games[$i] . "</td>" . "<br/>";
}
?>
</body>
</html>
梅里林
在代码中,必须在循环外使用
,然后定义5列,才能根据需要使用$games
数组:
示例:
<?php
$games = array(
"battlefield 1",
"battlefield 2",
"Out of the Park Baseball 17",
"Overwatch",
"Stephen's Sausage Roll",
"Dark Souls III",
"Kentucky Route Zero - Act IV NEW",
"Stardew Valley",
"Ori and the Blind Forest",
"XCOM 2",
"World of Warcraft: Legion",
"Steins;Gate ",
"The Witness",
"Inside",
"Hex: Shards of Fate",
"Forza Horizon 3 ",
"Rise of the Tomb Raider",
"Total War: WARHAMMER",
"Chime Sharp",
"Pony Island",
"F1 2016",
"Day of the Tentacle Remastered",
"Tales from the Borderlands",
"DOOM",
"Hearthstone");
?>
<table border="1">
<tr>
<?php
// this will print 5 columns
for ($i=1; $i <= 5 ; $i++) {
?>
<td><?php echo "Column". $i ?></td>
<?php
}
?>
</tr>
<?php
// this will print your games value in 5 rows each
$games = array_chunk($games, 5); // break in chunks
foreach ($games as $key => $value) {
echo "<tr>"; // starting tr for values
foreach ($value as $fvalue) { // this will break in 5 rows each.
?>
<td><?=$fvalue?></td>
<?php
}
echo "</tr>"; // closing tr
}
?>
</table>
可能不是最干净的,但它可以工作:)
试试这个:
$games = array(
"battlefield 1",
"battlefield 2",
"Out of the Park Baseball 17",
"Overwatch",
"Stephen's Sausage Roll",
"Dark Souls III",
"Kentucky Route Zero - Act IV NEW",
"Stardew Valley",
"Ori and the Blind Forest",
"XCOM 2",
"World of Warcraft: Legion",
"Steins;Gate ",
"The Witness",
"Inside",
"Hex: Shards of Fate",
"Forza Horizon 3 ",
"Rise of the Tomb Raider",
"Total War: WARHAMMER",
"Chime Sharp",
"Pony Island",
"F1 2016",
"Day of the Tentacle Remastered",
"Tales from the Borderlands",
"DOOM",
"Hearthstone");
echo '<table border=1 rows=5 cols=5><tr>';
for($i =0; $i < 25; $i++){
if ($i%5 === 0) {
echo '</tr><tr>';
}
echo "<td>" . $games[$i] . "</td>";
}
echo '</tr></table>';
$games=array(
“战场1”,
“战场2”,
“公园外棒球17”,
“看守”,
“斯蒂芬香肠卷”,
“黑暗灵魂III”,
“肯塔基州零号公路-第四幕新增”,
“星露谷”,
“Ori和盲林”,
“XCOM 2”,
“魔兽世界:军团”,
“斯坦斯;大门”,
“证人”,
“内部”,
“魔咒:命运的碎片”,
“地平线广场3号”,
“古墓丽影的崛起”,
“全面战争:战锤”,
“尖锐的钟声”,
“小马岛”,
“F1 2016”,
“触手日重拍”,
“来自边疆的故事”,
“厄运”,
“炉石”);
回声';
对于($i=0;$i<25;$i++){
如果($i%5==0){
回声';
}
echo“$games[$i]”;
}
回声';
u需要在循环外使用table,但这仍然只会打印1列和多行,并且您的长度应该是$i<25
,因为从0开始这是实现目标的好方法@devpro comment向您显示了一些需要修复的错误,然后您只需在每5场比赛后添加开始和结束。您可以使用$i检查它的模除5是否没有提示,这意味着该开始新的一行了。@Ranker抱歉,我不明白。你能给我一个提示或什么吗?他说你在循环内部使用,这是错误的,@Ranker,现在你有2个解决方案,试试看我得到了6行,不是5行。我的错,从1开始计数,而不是0,修复并更新了。你能解释我这是什么吗我不知道运算符$i%5==0($i=0;$i<25;$i++){if($i%5===0){echo'';}echo'.$games[$i]。;}$i%5==0坦克你,我是对的,它是一个操作员吗?如果是的话,你可能知道一个网站,他们在那里向我解释这个操作员吗?一定也喜欢一个演示;-)旁注:为了得到干净的HTML,最好在每个元素后面添加“\n”
。这样,HTML源代码就不会在一行中显示为一个大的块,这使得在需要查看HTML源代码时很难进行调试。
$games = array(
"battlefield 1",
"battlefield 2",
"Out of the Park Baseball 17",
"Overwatch",
"Stephen's Sausage Roll",
"Dark Souls III",
"Kentucky Route Zero - Act IV NEW",
"Stardew Valley",
"Ori and the Blind Forest",
"XCOM 2",
"World of Warcraft: Legion",
"Steins;Gate ",
"The Witness",
"Inside",
"Hex: Shards of Fate",
"Forza Horizon 3 ",
"Rise of the Tomb Raider",
"Total War: WARHAMMER",
"Chime Sharp",
"Pony Island",
"F1 2016",
"Day of the Tentacle Remastered",
"Tales from the Borderlands",
"DOOM",
"Hearthstone");
echo '<table border=1 rows=5 cols=5><tr>';
for($i =0; $i < 25; $i++){
if ($i%5 === 0) {
echo '</tr><tr>';
}
echo "<td>" . $games[$i] . "</td>";
}
echo '</tr></table>';