Count Array Elements and Find Min or Max Values

How to count elements of multidimensional or nested array, count the frequency of unique values in an array, and find the maximum and minimum values in an array.

  1. Counting elements of an array.
  2. Counting elements of a multidimensional or nested array.
  3. Count the frequency of unique values in an array.
  4. Finding the maximum and minimum values in an array.

Counting elements in arrays

//Count function syntax
count(Countable|array $value, int $mode = COUNT_NORMAL): int

The count( ) function returns the number of elements in the array, the following example prints 7 as expected:

$days = array("Mon", "Tue", "Wed", "Thu",
              "Fri", "Sat", "Sun");
echo count($days);  // 7

The count( ) function works on an array or countable object and returns 0 when either an empty array or a variable that isn’t set is examined. If there is any doubt, isset( ) and is_array( ) should be used to check the variable being considered.

Counting elements of a multidimensional or nested array

To find the number of elements in a multidimensional or nested array, the count() function has an optional mode argument COUNT_RECURSIVE that recursively counts the elements:

 //7 elements
 $a = array('sun','mon','tue','wed',
 //12 elements
 $b = array('jan','feb','mar','apr',
//2 elements
$c = array ('days' => $a, 'month' => $b);

echo count ($c); //2
echo count ($c, COUNT_RECURSIVE); //21

echo count ($c, 1); //21

sizeof vs. count

The sizeof() function is an alias for the count() function.

 $days = array("Mon", "Tue", "Wed", "Thu",
              "Fri", "Sat", "Sun");
 echo sizeof($days);  // 7

Count frequency of unique values in an array

array_count_values(array $array): array

The array_count_values() function counts how many times a unique value is found in an array. It returns an associative array with the keys representing the unique element of the array, and the value containing the number of times that unique element occurred within the array.

 $a = array(1, 3, 1, 5, 5, 9, 0, 5);
 $b = array_count_values( $a );
 $c = print_r ( $b , true);

 echo "<pre> $c </pre>";
    [1] => 2
    [3] => 1
    [5] => 3
    [9] => 1
    [0] => 1

This example counts the frequency of the values in the array. The array_count_values function returns an array that uses the value as an index and the frequency as the value.

Finding the maximum and minimum values in an array

The maximum and minimum values can be found from an array numbers with max( ) and min( ), respectively:

//max functin syntax
max(array $value_array): mixed

//min function syntax
min(array $value_array): mixed

If an array of integers is examined, the returned result is an integer, if an array of floats is examined, min( ) and max( ) return a float:

 $var = array(10, 5, 37, 42, 1, -56);
 echo max($var);  // prints 42
 echo min($var);  // prints -56

Both min( ) and max( ) can also be called with a list of integer or float arguments:

 echo max (10, 5, 37, 42, 1, -56); // prints 42
 echo min (10, 5, 37, 42, 1, -56); // prints -56

Both max( ) and min( ) work with strings or arrays of strings, but the results may not always be as expected.

Working with arrays:

  1. Creating Arrays
  2. Array Iteration
  3. Array Internal Pointers
  4. Explode and Implode Functions
  5. Count Array Elements, Find Min and Max Values
  6. Search values and keys in arrays
  7. Filter array elements using array_filter()
  8. Modify array elements using array_map() and array_walk()
  9. Split, tokenize, and iterate a string
  10. Convert a delimited string into an array
  11. Sorting arrays