Example 1 — Counting Repeated Values
Count how many times each fruit appears in a list.
<?php
$fruits = ['apple', 'banana', 'apple', 'cherry', 'banana', 'apple'];
print_r(array_count_values($fruits)); How It Works
- The list has six fruits, and some of them repeat.
- array_count_values() goes through the list and counts each distinct value.
- 'apple' appears three times, 'banana' twice and 'cherry' once.
- The result keeps the order in which each fruit first appeared.

