Use foreach to loop associative array and get key and value in PHP

Description

The following code shows how to use foreach to loop associative array and get key and value.

Example


<!DOCTYPE html>//  w  w  w .j  a v  a2s  .co  m
<html>
  <body>
    <h1>Using foreach</h1>
    <dl>
    <?php
    
    $myBook = array( "title" => "XML",
                     "author" => "Jack",
                     "pubYear" => 2014);
    
    foreach ( $myBook as $key => $value ) {
      echo "<dt>$key</dt>";
      echo "<dd>$value</dd>";
    }
    
    ?>
    </dl>
  </body>
</html>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Language Basic »




PHP Introduction
PHP Operators
PHP Statements
Variable
PHP Function Create
Exception
PHP Class Definition