Some Handy Array Functions in PHP

This post will go over the basics of some very nice array-related functions that are built in to PHP. If you use arrays fairly often in your PHP code, you will no doubt find a use for each of these handy functions over time.

array_combine

This function takes two arrays and puts them together, though not in the way you might think at first. This function takes the first array and uses it as the keys for your new array. It then takes the second array and uses it as the values for your new array. It is important to ensure that both of your input arrays have the same number of elements. If they don’t, the function will return boolean FALSE rather than returning your new array.

Let’s say you start off by creating an array of all of the abbreviations for the calendar months:

$cal_month_abb = array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');

Next, you create a new array with all of the long names of those months:

$cal_month_names = array('January','February','March','April','May','June','July','August','September','October','November','December');

Now, imagine that when you created those two separate arrays, it made sense to keep them as two separate arrays because you needed to access the elements of the arrays numerically. Because you created non-associative arrays, each item in the arrays was automatically assigned a numerical key, starting at 0 and increasing by 1 for each element you added.

However, as you begin to code, you realize that it would also be helpful to be able to translate the abbreviations into the full names of the months or vice-versa. There’s no need to go through a complicated loop associating the abbreviations with the month names, nor is there any reason for creating a whole new array from scratch. Instead, you can simply use the array_combine function to put them together.

$cal_month_array = array_combine($cal_month_abb,$cal_month_names);

Once you run the PHP code above, you will end up with an array that looks like:

array(
'Jan'=>'January',
'Feb'=>'February',
'Mar'=>'March',
'Apr'=>'April',
'May'=>'May',
'Jun'=>'June',
'Jul'=>'July',
'Aug'=>'August',
'Sep'=>'September',
'Oct'=>'October',
'Nov'=>'November',
'Dec'=>'December');

I’m sure there are many more examples of how to use the array_combine function in real life coding. Hopefully, when you come across some examples, you’ll share them with us.

array_keys and array_values

These two functions do the opposite of array_combine. The first function, array_keys, extracts the keys from an associative array and stores them into a new non-associative array (an array with consecutive numerical keys, starting at 0) as the values. The second function, array_values, extracts just the values from an associative array and stores them in a new non-associative array.

So, let’s imagine that you started with the $cal_month_array shown in the example above. Now, you decide you want to be able to use the month abbreviations on their own, and you need to access them numerically. You would simply use:

$cal_month_abb = array_keys($cal_month_array);

If you wanted to access the month names numerically, you would run:

$cal_month_names = array_values($cal_month_array);

array_flip

Now, let’s say that you find your users are referring to months by their whole names, and you want to figure out a way to convert those to the abbreviations. You’ve already got the $cal_month_array array with the month abbreviations as the keys and the month names as the values. However, you need the month abbreviations to be the values in the array and the month names to be the keys. All you need is the array_flip function.

The array_flip function takes the keys and transposes them with the associated values. Therefore, if you were to run:

$new_array = array_flip($cal_month_array);

You would end up with an array that looks like:

array(
'January'=>'Jan',
'February'=>'Feb',
'March'=>'Mar',
'April'=>'Apr',
'May'=>'May',
'June'=>'Jun',
'July'=>'Jul',
'August'=>'Aug',
'September'=>'Sep',
'October'=>'Oct',
'November'=>'Nov',
'December'=>'Dec');

array_key_exists

You can use the array_key_exists function to figure out whether a specific key exists within an array. It is always a good idea to check to make sure that a key exists before trying to do something with that array item. If you try to use an array item that doesn’t exist, PHP will output a warning message.

Therefore, before trying to do something like:

echo $cal_month_array['Jan'];

You need to check to make sure that an array item exists with the key “Jan”. You can, of course, check the array item by using the isset function, but isset will return boolean FALSE for array items that have been set to NULL, while array_key_exists will return boolean TRUE (because the key does still exist, it’s just associated with NULL instead of a value). As far as I know, isset will also return boolean FALSE if you have the key’s value set to boolean FALSE or 0.

This can be an extremely sticky situation, depending on what you’re pushing into your arrays, so make sure you use the appropriate function for the job.

array_pop

If you need to grab the last item off of the end of an array, you can use the array_pop function to do so. The array_pop function simply “pops” the last element off of an array. It is important to note that this function will actually remove the last element from the array. Therefore, you should not use this function if you just want to know what the last element is in an array.

However, if you actually want to take the last element out of the array, this is the function you should use.

When might you use something like this? Let’s say you have people supplying their full names on a form. Once they submit the form, you want to return the person’s name with their last name first, then a comma, then the rest of their name. You can do this with a combination of a few handy array functions. It might look something like:

$name_array = explode(' ',$_POST['yourname']); /* Will split the string into an array, separating it wherever a space is found
$lastname = array_pop($name_array) /* Will remove the last word from the array and store it as a variable called $lastname
echo $lastname.', '.implode(' ',$name_array); /* Will print the $lastname variable, then a comma, then will print the rest of the words in the name with spaces in between

explode and implode

In the above example, I used two handy functions called explode and implode. These functions are extremely useful for all kinds of things.

The explode function will take a string input and split it into an array, separating it wherever the “delimiter” is found in the input string. This can be extremely handy for turning comma-separated lists into arrays. This function is very similar to the “split” function found in many other languages. However, split is a slightly different function in PHP, and should only be used if you need to use a regular expression as your “delimiter”.

The implode function will take an array input and combine it into a string. This is similar to the “join” function found in many other languages. In fact, the PHP join function is simply an alias of the implode function.

Conclusion

Hopefully you will find some use for the functions I’ve explained above. In a future post, I will go over many of the various functions you can use to sort your arrays in PHP.