Core PHP

Associative Arrays

An associative array holds a list of data items sequentially that are selected and accessed via a label.

The program below creates an associative array called: $saSaintFeastDays. This array associates the name of a saint with the feast day of the saint.

AssociativeArrays.php

<?php
  $saSaintFeastDays = array("Justin"=>"April 14", "Mark"=>"April 25", "Stephen"=>"December 26");

  // Loop over the entries of the array and print them
  foreach($saSaintFeastDays as $sSaint => $sDay) {
    echo "Key=".$sSaint." | Value=".$sDay."<br>";
    echo "The feast day of Saint ".$sSaint." is ".$sDay.".<br><br>";
  }

  echo "The size of the array is ".count($saSaintFeastDays).".<br><br>";
?>
 

Output

 
 

© 2007–2024 XoaX.net LLC. All rights reserved.