<?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"; } ?>