Advertisement
eimkasp

PHP foreach example

Mar 23rd, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.53 KB | None | 0 0
  1. <?php
  2.     $persons = [
  3.     [
  4.         'name'    => 'John',
  5.         'surname' => 'Doe',
  6.         'age' => 55,
  7.     ],
  8.     [
  9.         'name'    => 'Ted',
  10.         'surname' => 'Smith',
  11.         'age' => 12,
  12.     ],
  13.     [
  14.         'name'    => 'Gorge',
  15.         'surname' => 'Doe',
  16.         'age' => 100,
  17.     ]];
  18. ?>
  19.  
  20.  
  21.  
  22.  
  23. <?php for($i = 0; $i < count($persons); $i++) {
  24.     echo $persons[$i];
  25. } ?>
  26.  
  27. <table>
  28. <?php foreach($persons as $person) : ?>
  29.     <tr>
  30.         <td><?= $person['name']; ?></td>
  31.         <td><?= $person['surname']; ?></td>
  32.         <td><?= $person['age']; ?></td>
  33.     </tr>
  34. <?php endforeach; ?>
  35. </table>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement