Getting and Displaying Counts of Array Values : array_values « Data Structure « PHP






Getting and Displaying Counts of Array Values

 
<html>
<head>
<script type="text/javascript">
</script>
</head>
<body>
<?php
  $countries = array( 'USA' => 'English', 'Spain' => 'Spanish',
                      'France' => 'French', 'Argentina' => 'Spanish');

  $languages = array_values($countries);
  $language_count = array();

  foreach($languages as $language)
    if(!isset($language_count[$language]))
      $language_count[$language] = 1;
    else
      $language_count[$language]++;

?>
  <table>
    <tbody>
      <tr><th>Language</th><th>Number<br />of<br />Countries</th></tr>
<?php
  foreach($language_count as $language => $number)
    print "<tr><td>$language</td><td>$number</td></tr>\n";
?>
    </tbody>
  </table>
</body>
</html>
  
  








Related examples in the same category

1.Return all the values of an array
2.array_values( ) returns an array of all the values in that array
3.array_values() function returns an array containing all of the values constituting the input array.