Useful array functions
PHP has a lot of functions that deal with arrays, as you can see by visiting the array section of the PHP manual. Let's see a few examples of using some of the most common functions. Do not try to remember them all, this is more of an informational article to demonstrate some of the possibilities. When the need arises, you could always visit the PHP manual and find the most appropriate function for your specific need.
A simple array
Let's start by defining a simple array and printing out its contents.
(All examples assume that the code is enclosed in "<pre>" tags)
$a = array( 'one' => 1, 'three' => 3, 'two' => 2, 'four' => 4 ); // print the array print_r($a);
The result is simply:
Array
(
[one] => 1
[three] => 3
[two] => 2
[four] => 4
)
Checking for existence
You can use the function isset() that checks whether a variable (or an array index) is defined.
// key exists
if (isset($a['one'])) {
echo 'key "one" exists';
}
This will print:
key "one" exists
Using empty() you can check if the key exists (same as isset()) but also if the array value with this key is a non-null value, in other words if something else than 0, "", null or FALSE,
// key exists and its value is not 0, "", null or FALSE
if (!empty($a['one'])) {
echo 'key "one" exists and has a value';
}
In our example array, array element with key "one" exists and its value is "1", so it's a non-null value. The code above will print:
key "one" exists and has a value
The function in_array() checks if a specified value exists in a given array. Let's check if the value 4 exists in the $a array:
// value exists
if (in_array(4, $a)) {
echo '4 is a value in the array';
}
The value 4 exists, so the code will print:
4 is a value in the array
Implode, explode
implode() and explode() are two very useful functions that turn an array into a string and vise versa. Let's see how.
implode() takes an array and a string (called delimiter) and returns a new string that has all the elements of the array with the delimiter string between them. Let's use the pipe symbol | as a delimiter:
echo implode("|", $a);
The result is:
1|3|2|4
Now let's see the opposite - we have a string and want to produce an array out of it. In the string "here,there,everywhere" we can use the comma as a delimiter:
$str = 'here,there,everywhere';
$b = explode(",", $str);
print_r($b);
Using explode() will create an array where the first element is "here", the second is "there" and the third is "everywhere". So the result of the code above will be:
Array
(
[0] => here
[1] => there
[2] => everywhere
)
Keys, values, reverse
Continuing with the useful array functions, let's see the functions array_keys(), array_values() and array_reverse().
array_keys() will take an array and return a new (enumerated) array, containing only the keys of the input array. array_values() will do the opposite - return a new array with the values only, so you'll lose the original keys. array_reverse() takes an array and reverses the order pf the elements, the last element becomes first and so on. It keeps the keys pointing to the same values.
// keys only print_r(array_keys($a)); // value only print_r(array_values($a)); // reverse the array print_r(array_reverse($a));
The code above will print:
Array
(
[0] => one
[1] => three
[2] => two
[3] => four
)
Array
(
[0] => 1
[1] => 3
[2] => 2
[3] => 4
)
Array
(
[four] => 4
[two] => 2
[three] => 3
[one] => 1
)
... and counting
The count() returns the number of elements in an array, useful when using for loops for example.
// count the elements echo count($a); // prints 4
Random stuff
shuffle() takes an array and randomizes its elements.
// shuffle $b = array_keys($a); shuffle($b); print_r($b);
This will produce something like.
Array
(
[0] => four
[1] => three
[2] => one
[3] => two
)
Another way to get random elements from an array is to use the function array_rand()
Sorting
There are quite a few functions that sort arrays - some sort ascending, some descending, some sort keys, some sort values, some keep the original keys of the array, some "re-key" the input array. There are also 3 functions that let you define custom sorting.
Let's see some of the built-in sorting functions.
sort() orders the elements of the array in ascending order (smallest first) and re-keys the array.
// sort ascending $b = $a; sort($b); print_r($b);
Note that we used a new value $b, this is because sort() will simply return a new modified array, but will actually change the input array, so we lose the original when we pass it to sort(). The code above prints:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
rsort() is like sort(), only it sorts the element in descending order. ("r" for "reversed")
// sort descending $b = $a; rsort($b); print_r($b);
The code above prints:
Array
(
[0] => 4
[1] => 3
[2] => 2
[3] => 1
)
"k" stands for "key". So ksort() is like sort() but it sorts keys of the array.
// sort keys $b = $a; ksort($b); print_r($b);
Array
(
[four] => 4
[one] => 1
[three] => 3
[two] => 2
)
Since "k" is "key" and "r" is "reverse" what would be the function to sort the elements in descending order, ordered by key? Mhm, krsort().
Summary
So you get the idea, there are tons of array functions, some you'll use quite often so that you'll memorize their names and attributes. Other functions you'll use less often, but the PHP manual is always there for you to consult.