简体   繁体   中英

PHP Multi dimentional array

This is my code. I don't know why it is not working. I was trying to print my table with php for loop, but nothing shows up on the website. Nothing

This is the two-dimentional array that I was trying to print out.

 <!--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")
 );
 ?>

This is the code that I was trying to use to print out.

<!--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>

You have to use foreach as foreach (array_expression as $value)

The foreach construct provides an easy way to iterate over arrays. foreach works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable.

Like:

<?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) {?>
      <tr>
          <?php foreach ( $weapon as $val ) {?>
           <td><?php echo $val;?></td>
          <?php }?>
      </tr>
      <?php }?>
</table>

This will result to:

在此处输入图片说明

Doc: foreach

Enhancing Eddie's answer, using foreach as well,
note that you could visually simplify the code like that:

<!--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>

Why using this solution?
Because the multiple opening and closing of php tags can make the code hard to read.

Documentation about foreach : http://php.net/manual/en/control-structures.foreach.php

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM