Loops through an indexed array of fruit names and creates a new array with the count of each fruit name. : Array Index « Data Structure « PHP






Loops through an indexed array of fruit names and creates a new array with the count of each fruit name.

 
<?php
$fruits = array('apple', 'orange', 'orange');
$fruit_count = array();
foreach ($fruits as $i=>$fruit) {
  @$fruit_count[$fruit]++;
}
asort($fruit_count);
foreach ($fruit_count as $fruit=>$count) {
  echo "$fruit = $count\n";
}
?>
  
  








Related examples in the same category

1.Adding elements with []
2.Alternating table row colors
3.Setting an Array's Size
4.Getting array size