count() / array_sum() / range() Since: PHP 4(2000)
Functions for counting array elements, computing totals, generating sequential arrays, and randomly manipulating arrays.
Syntax
// Returns the number of elements in an array. count(array, mode); // Returns the sum of values in an array. array_sum(array); // Returns the product of values in an array. array_product(array); // Generates a sequential array. range(start, end, step); // Shuffles the elements of an array in random order. shuffle(array); // Returns one or more random keys from an array. array_rand(array, count);
Function List
| Function | Description |
|---|---|
| count($array, $mode) | Returns the number of elements in an array. Pass COUNT_RECURSIVE as the second argument to recursively count all elements in a multidimensional array. |
| array_sum($array) | Returns the sum of all values in an array. Non-numeric values are treated as 0. |
| array_product($array) | Returns the product of all values in an array. Returns 1 for an empty array. |
| range($start, $end, $step) | Generates an array containing a sequence of values from start to end. Also works with strings. |
| shuffle($array) | Randomly reorders the elements of an array. Keys are re-indexed starting from 0. |
| array_rand($array, $num) | Returns one or more random keys from an array. |
Sample Code
<?php
// Get the number of elements in an array.
$fruits = ['apple', 'orange', 'grape', 'peach'];
echo count($fruits); // Outputs "4".
// Calculate the sum of an array.
$prices = [150, 80, 300, 200];
echo array_sum($prices); // Outputs "730".
// Calculate the product of an array.
$nums = [2, 3, 4];
echo array_product($nums); // Outputs "24".
// Calculate the average.
$scores = [85, 92, 78, 95, 88];
$average = array_sum($scores) / count($scores);
echo $average; // Outputs "87.6".
// Generate a sequential array.
$one_to_ten = range(1, 10);
print_r($one_to_ten); // Generates an array from 1 to 10.
// Generate an array of even numbers using a step value.
$evens = range(2, 10, 2);
print_r($evens); // Results in [2, 4, 6, 8, 10].
// Generate an array of letters.
$alphabet = range('A', 'F');
print_r($alphabet); // Results in ['A', 'B', 'C', 'D', 'E', 'F'].
// Shuffle an array randomly.
$cards = range(1, 5);
shuffle($cards);
print_r($cards); // Elements are in a random order.
// Pick a random element from an array.
$colors = ['red', 'blue', 'green', 'yellow', 'purple'];
$key = array_rand($colors);
echo $colors[$key]; // Outputs one random color.
// Pick multiple random keys.
$keys = array_rand($colors, 2);
print_r($keys); // Returns two randomly selected keys.
Notes
count() can be used not only on arrays but also on objects that implement the Countable interface. Since PHP 7.2, passing a value that is neither an array nor a Countable object triggers a warning. The alias sizeof() also exists, but count() is more commonly used.
array_sum() also works on the values of associative arrays. Non-numeric values are treated as 0. Combining array_sum() and count() makes it easy to calculate an average.
range() can generate sequences of strings as well as numbers. Passing a negative step value creates a descending array.
shuffle() modifies the original array in place and re-indexes keys starting from 0. array_rand() returns keys, not values, so you need to use the returned key to access the corresponding array element. For aggregating array values, see array_reduce().
If you find any errors or copyright issues, please contact us.