In the previous chapter, we discussed how to create arrays and fill them with keys and values. In this chapter, we will take a look at some of the functions which makes working with arrays much easier.
Implode and explode
Implode() and explode() are two very cool string functions which are related to arrays. They allow you to make an array out of a string and the other way around. Here is an example where we use both functions:
<?php $values = "Rabbit|Whale|Penguin|Bird"; $array = explode("|", $values); print_r($array); echo "<br /><br />"; $string = implode(" and ", $array); echo $string; ?>
First of all, we define a string with a set of animals listed in it. We separate each with a |. We then use the explode function to split the values of the string into an array - each time the | character is met, the following value is appended as a new value to the array. We use the print_r() function to make a test output of the array, to see that it actually works. After that, we use the implode() function to join each element in the array. We have specified " and " as the first parameter, which means that each time an element is appended to the string, the " and " is appended as well. Try testing the code to see the result. Explode() and implode() can really help you out in a lot of situations.
Is a value in the array?
Checking whether or not a given value is in the array can quickly come in handy. Consider the following example, where we use the in_array() function to check:
<?php $animals = array("Dog", "Tiger", "Snake", "Goat"); if(in_array("Snake", $animals)) echo "Snake is in the array!"; else echo "No snake in the array!"; ?>
The function can take up to three parameters, where the last one is optional. It specifies whether or not the comparison should be strict, that is, both the value and the type must match. The default parameter is false. This function checks for values. If you wish to verify whether or not a key exists in an array, use the array_key_exists() instead.
Sometimes you will get arrays from an external source, e.g. a database or a file. If duplicate entries might occur, you may use the array_unique() function to remove them. This function will return an array where all values are unique. For instance, try this example:
<?php $array = array("Dog", "Tiger", "Snake", "Dog"); echo "Animals in array:<br />"; foreach($array as $value) echo $value . "<br />"; echo "<br />"; $array = array_unique($array); echo "Animals in unique array:<br />"; foreach($array as $value) echo $value . "<br />"; ?>
Getting a random element
On many occasions you may need to get a random value from a list of options. For instance, you could have one of those "Random quotes" functions. The easiest way to do this is to gather up an array of your quotes, and then select a random entry each time. Fortunately, PHP makes it easy, with the array_rand() function. Here is an example:
<?php $animals = array("Dog", "Tiger", "Snake", "Goat"); $randomAnimal = $animals[array_rand($animals, 1)]; echo "Random animal: " . $randomAnimal; ?>
The array_rand() comes in two different flavors: If you specify 1 as the last parameter (or nothing at all, since it's the default value), a key will be returned which will give you access to the random element in your array. However, you may need more than one random element back. In that case, simply specify the amount as the second parameter. In that case, an array containing the amount of random values will be returned.
Use the sort() function to sort a complete array, like this example:
<?php $animals = array("Dog", "Tiger", "Snake", "Goat", "Rabbit", "Whale", "Bird"); echo "Unsorted animals: " . implode(", ", $animals); echo "<br /><br />"; sort($animals); echo "Sorted animals: " . implode(", ", $animals); echo "<br /><br />"; $animals = array_reverse($animals); echo "Sorted animals, descending order: " . implode(", ", $animals); ?>
We simply define an array with a mixed set of animals in it. Then we output it three times. First in the original order, then sorted, using the sort() method, and then after using the array_reverse() function, which simply turns the array around.