What is the purpose of counting and sorting array contents in PHP, and how can it be achieved efficiently?

Counting and sorting array contents in PHP is important for organizing and analyzing data. Counting allows you to determine the number of elements in an array, while sorting helps in arranging the elements in a specific order. This can be achieved efficiently using built-in PHP functions like count() for counting and functions like sort() or asort() for sorting.

// Counting the number of elements in an array
$array = [1, 2, 3, 4, 5];
$count = count($array);
echo "Number of elements in the array: " . $count;

// Sorting an array in ascending order
$numbers = [5, 3, 8, 1, 2];
sort($numbers);
print_r($numbers);