PHP provides a number of library functions for working on arrays.
Counting the Elements in an array
The count() function returns the number of elements in an array. It will return zero if the array is empty or the array variable has not been set.
Example of count()
<?php
$names = array ("Tom", "Dick", "Harry");
// This will print out 3
echo count ($names);
?>
Finding the Maximum or Minimum Values in an array
The max() and min() functions will return the maximum and minimum values in an array respectively.
Both can be used with integers or floats and will return the appropriate type. They can be used with strings but may not return the results you expect.
Example of max() and min()
<?php
$numbers = array (40, 23, -10, 4);
// This will print out Maximum 40 Minimum -10
echo ('Maximum '.max($numbers).' Minimum '.min($numbers));
?>
Finding Values in arrays
PHP provides the two functions in_array() and array_search().
in_array()
This function returns true if an array contains a specific value.
Example of in_array()
<?php
$names = array ("Tom", "Dick", "Harry");
// This will print out Harry is present
if (in_array ("Harry", $names));
echo ("Harry is present");
?>
The function also allows a third parameter that causes a strict type check to be made.
in_array() With and Without Type Checking
<?php
$numbers = array (10, 20, 30, 40, 50);
// This will print out 40 is present although the types are different
if (in_array ("40", $numbers));
echo ("40 is present");
// This won't print anything because the types are different
if (in_array ("40", $numbers, true));
echo ("40 is present");
?>
array_search()
The array_search() function works in the same way as the in_array() function except that if the value is found it returns the key of the matching value instead of a Boolean value true.
If the value isn't found then false is returned.
Note: If the function is used on a numbered array and it finds the value in the first element then it will return 0 and this will evaluate as false unless the is_identical operator is used.
Example of array_search()
<?php
$numbers = array (10, 20, 30, 40, 50);
/* This will find the first element of the array
but will return 0 which will evaluate as false
therefore 10 is present will not be printed out */
if (in_array (10, $numbers));
echo ("10 is present");
// If is_identical is used it will work
$index = in_array (10, $numbers);
if ($index === false);
echo ("10 is not present");
else
echo ("10 is present");
?>
Reversing arrays
The array_reverse() function allows you to create a new array from an existing array with all its elements in reverse order.
Example of array_reverse()
<?php
$countup = array (1, 2, 3, 4, 5);
// Print out the indexes and element values
foreach ($countup as $index => $number)
echo('['.$index.']'.$number.', ');
// Which gives [0]1, [1]2, [2]3, [3]4, [4]5,
// Now reverse the order of elements in the array
$countdown = array_reverse ($countup);
// Print out the indexes and element values
foreach ($countdown as $index => $number)
echo('['.$index.']'.$number.', ');
// Which gives [0]5, [1]4, [2]3, [3]2, [4]1,
?>
The array_reverse() function accepts an optional additional Boolean argument called preserve_key which maintains the relationship between the elements and the index.
Example of array_reverse() with preserve_key
<?php
$countup = array (1, 2, 3, 4, 5);
// Reverse the order of elements in the array but preserve the key
$countdown = array_reverse ($countup, true);
// Print out the indexes and element values
foreach ($countdown as $index => $number)
echo('['.$index.']'.$number.', ');