Functions for performing mathematical calculations on arrays

array_sum($array)
array_product($array)

How to add all values in an array

$prices = array(141.95, 212.95, 411, 10.95);
$sum = array_sum($prices);

Output

sum = 776.85

Functions for searching arrays

in_array($value, $array [, $strict])
array_key_exists($key, $array)
array_search($value, $array [, $strict])
array_count_values($array)

How to search an array

$tax_rates = array('NC' => 7.75, 'CA' => 8.25, 'NY' => 8.875);
$is_found = in_array(7.75, $tax_rates); // TRUE
$is_found = in_array('7.75', $tax_rates); // TRUE
$is_found = in_array('7.75', $tax_rates, true); // FALSE
$key_exists = array_key_exists('CA', $tax_rates); // TRUE
$key = array_search(7.75, $tax_rates); // 'NC'

How to count the number of occurrences of a value in an array

$names = array('Mike', 'Mike', 'Mike', 'Anne', 'Joel', 'Joel');
$occurences = array_count_values($names);
echo($occurences['Mike']); // 1
echo($occurences['Anne']); // 3
echo($occurences['Joel']); // 3

Output

Mike: 3
Anne: 1
Joel: 2

Back