Search notes:

PHP code snippets: foreach

foreach can be used to iterate over an array.

Values

foreach($ary as $val) iterates over the elements in an array and assigns each element's value to $val.
<html><head><title>Print indices and values of an array</title></head>
<body>

  <?php 
   
    $ary = array('one', 'two', 'three');
    
    foreach ($ary as $index => $val) {
        print "<br>$index: $val";
    }

  ?>

</body>
</html>
Github repository about-php, path: /array/foreach/indices-values.html

Indices and values

Similarly, foreach($ary as $index => $val) iterates over the elements in an array and assigns the each element's index to $index and their values to $val.
<html><head><title>Print indices and values of an array</title></head>
<body>

  <?php 
   
    $ary = array('one', 'two', 'three');
    
    foreach ($ary as $index => $val) {
        print "<br>$index: $val";
    }

  ?>

</body>
</html>
Github repository about-php, path: /array/foreach/indices-values.html

See also

Other PHP snippets

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1739655390, '3.144.117.125', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/web/php/snippets/array/foreach(84): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78