Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP多维数组_Php_Html_Arrays_For Loop - Fatal编程技术网

PHP多维数组

PHP多维数组,php,html,arrays,for-loop,Php,Html,Arrays,For Loop,这是我的密码。我不知道为什么它不起作用。 我试图用php for loop打印我的表,但网站上什么也没有显示 这是我想要打印出来的二维数组 <!--Arrays of weapons--> <?php $weapons = array( array("M4A1",1,78906,"TUCKER, LISBETH","SPC"), array("M4A1",2,78915,"HAT

这是我的密码。我不知道为什么它不起作用。 我试图用php for loop打印我的表,但网站上什么也没有显示

这是我想要打印出来的二维数组

 <!--Arrays of weapons-->
 <?php
 $weapons = array(
 array("M4A1",1,78906,"TUCKER, LISBETH","SPC"),
 array("M4A1",2,78915,"HATHAWAY, HANNAH","1LT"),
 array("M4A1",3,78933,"HARRIS, LEE","SFC"),
 array("M4A1",4,78934,"WELCH, BRAD","SSG"),
 array("M9",1,1167552,"BLAND, MARGARET","CPT"),
 array("M249",1,101032,"TYSON, MICHELLE","1SG"),
 array("M249",2,101038,"MEDINA, TOBIAS","SPC"),
 array("M240B",1,104104,"COSTA, JOSHUA","SSG"),
 array("M2A1",1,1863848,"GARCIA, RIGOBERTO","SSG"),
 array("MK-19",1,19369,"NEUPANE, KISHOR","SPC")
 );
 ?>
<!--Create the Weapons List Table-->
 <table border ="1">
 <tr>
  <th>Type</th>
  <th>Buttstock #</th>
  <th>Serial #</th>
  <th>Name</th>
  <th>Rank</th>
 </tr>
 <!--Put two-dimentinal arrays in the table-->
 <?php foreach ($row = 0; $row < 10, $row++) {?>
  <tr>
  <?php for ($col = 0; $col < 5, $col++) {?>
   <td><?php echo $weapons[$row][$col];?></td>
  <?php }?>
  </tr>
  <?php }?>
 </table>

这是我试图打印出来的代码

 <!--Arrays of weapons-->
 <?php
 $weapons = array(
 array("M4A1",1,78906,"TUCKER, LISBETH","SPC"),
 array("M4A1",2,78915,"HATHAWAY, HANNAH","1LT"),
 array("M4A1",3,78933,"HARRIS, LEE","SFC"),
 array("M4A1",4,78934,"WELCH, BRAD","SSG"),
 array("M9",1,1167552,"BLAND, MARGARET","CPT"),
 array("M249",1,101032,"TYSON, MICHELLE","1SG"),
 array("M249",2,101038,"MEDINA, TOBIAS","SPC"),
 array("M240B",1,104104,"COSTA, JOSHUA","SSG"),
 array("M2A1",1,1863848,"GARCIA, RIGOBERTO","SSG"),
 array("MK-19",1,19369,"NEUPANE, KISHOR","SPC")
 );
 ?>
<!--Create the Weapons List Table-->
 <table border ="1">
 <tr>
  <th>Type</th>
  <th>Buttstock #</th>
  <th>Serial #</th>
  <th>Name</th>
  <th>Rank</th>
 </tr>
 <!--Put two-dimentinal arrays in the table-->
 <?php foreach ($row = 0; $row < 10, $row++) {?>
  <tr>
  <?php for ($col = 0; $col < 5, $col++) {?>
   <td><?php echo $weapons[$row][$col];?></td>
  <?php }?>
  </tr>
  <?php }?>
 </table>

类型
枪托#
连载#
名称
等级

您必须将
foreach
用作
foreach(数组表达式为$value)

foreach构造提供了一种在数组上迭代的简单方法。 foreach只在数组和对象上工作,当 您尝试在具有不同数据类型或 未初始化的变量

比如:


类型
枪托#
连载#
名称
等级
这将导致:


医生:

加强埃迪的回答,同时使用
foreach

请注意,您可以直观地简化代码,如下所示:

<!--Arrays of weapons-->
<?php
$weapons = array(
  array("Type 1",1,78906,"Apple","R1"),
  array("Type 2",2,78915,"Javascript","R4"),
  array("Type 3",3,78933,"Red","R6"),
  array("Type 4",4,78934,"Circle","R1"),
  array("Type 5",1,1167552,"Fried rice","R4"),
);
?>
<table border ="1">
 <tr>
  <th>Type</th>
  <th>Buttstock #</th>
  <th>Serial #</th>
  <th>Name</th>
  <th>Rank</th>
 </tr>
 <!--Put two-dimentinal arrays in the table-->
 <?php
  foreach ($weapons as $weapon) {
    echo '<tr>';
    foreach ( $weapon as $val ) {
        echo "<td>$val</td>";
    }
    echo '</tr>';
  } ?>
</table>

类型
枪托#
连载#
名称
等级
为什么要使用此解决方案?
因为多次打开和关闭php标记会使代码难以阅读

有关foreach的文档:


希望有帮助。

@HoseokLee,您似乎正在尝试删除示例数据。我用一些虚拟数据替换了它,但是编辑历史仍然可用。