Associative Array

This is a simple nested associative array and how to display the elements. In this array, we have three nested arrays. We will loop through using an array $key to display the elements within the array. So we will create a foreach loop within another foreach loop to get the elements.
  1. <?php
  2. // define associative array
  3. // associative array within associative array
  4. $data = array("UK" => array("longname" => "United Kingdom", "currency" => "GBP"),"US" => array("longname" => "United States of America", "currency" =>"USD"),"BD" => array("longname" => "Bangladesh", "currency" => "BDT"));
  5.  
  6. // print array contents
  7.  
  8. foreach($data as $key=> $val)
  9. {
  10. foreach($val as $key=> $t)
  11.  echo $key.':'.' '.$t.'<br>';  
  12. }
  13. ?>
Tags

Add new comment