The Top 17 Most Popular PHP Array Functions

Popular PHP Array Functions

You are going to run into PHP Arrays and PHP array functions with amazing regularity during the course of your web design and web development. It makes sense, because arrays are one of the most useful data types we can use. So it got us to thinking, what would you get if you combined the worlds most popular PHP CMS applications and dumped all of their source code into one bucket, then analyzed their most used array functions? Well you would get this awesome list of course! This is a great way to keep your skills sharp or simply refreshed when working with Arrays in PHP. Let’s check it out.

1. array()

This handy little guy creates an array for you. Here we’ll create an associative array with two keys, and then place an an associative array inside one and an indexed array inside the other.

And it goes a little something like this, HIT IT:

Learn more about array() at

2. is_array()

Checks whether the variable is an array. Returns TRUE if the variable is an array, and FALSE otherwise. Used like so:

Learn more about is_array() at

3. in_array()

We may often want to check if a certain value is in one of our arrays. Recall from our example above that we assigned an indexed array of friends to the ‘friends’ key of our example array. Let’s see if ‘Jeff’ was included.

Ah ha, turns out he was. With in_array(), the first argument is what you are looking for, and the second argument is the array you will check in. Learn more about in_array() at

4. array_merge

Remember we had some websites and friends in the array from the prior example. In fact we stored an array of values in those arrays. Well I’m feeling lazy and I don’t want to work with two arrays. I just want to work with one, so let’s merge those two arrays together!

And bingo bango, we now have one array created out of two!

Learn more about array_merge() at

5. array_keys

Let’s now extract all the keys from our $merged array by adding these lines:

And now you can see the keys of our $merged array become the values of the array returned from array_keys()!

Learn more about array_keys() at

6. array_key_exists()

With array_key_exists() we can perform a validation similar to the way we would with isset(). You pass a key to search for and an array to search in, and the function will return TRUE if the key exists in the array provided. Building on our prior example, let’s see if the 7th key is set:

Learn more about array_key_exists() at

7. array_shift()

Let’s remember our original $array which had two keys in it, ‘websites’ and ‘friends’. I want each key to have it’s own variable name in the program, how can I do that? Well, let’s apply the array_shift() function to $array, return the result to a variable $shifted, and examine both variables.

You can see the ‘websites’ key, which is the first in the array, was shifted out of the array, while the ‘friends’ key was left intact. Awesome!

Learn more about array_shift() at

8. array_push

Our newly created $shifted variable is mad. It wants back into the original $array variable to undo the shifting we have caused. Well, let’s try to apply the array_push() function to the original $array and push $shifted back onto the end of the array:

You can see friends is now in the first position and the $shifted array is added back to the original array. Note that array_push() does not keep key => value pairs intact! We have lost our ‘websites’ key(it is now 0).

Learn more about array_push() at

9. array_pop

Fickle mister $shifted is mad about losing his ‘websites’ key. I’m not going to be in this $array if I can’t have my original key. Ok fine then, we’ll array_pop() you right off the end of this $array one more time.

And there you go mister $shifted – popped off and placed back into your own array.

Learn more about array_pop() at

10. array_values

Remember when we created the $merged array earlier and we got a nifty little array looking like this?

Well, I’m not crazy about the fact that some of those keys are named and others are indexed. I’d like to grab just the values of the $merged array listed only with numeric indexes. Let use the array_values() function to do just that.

Ah yes, there she is – my nicely formatted merged array with neat indexes 0 – 7 courtesy of array_values().

Learn more about array_values() at

11. array_map

Ok all this array business has me needing a break. Let’s go shopping and buy some cool new gadgets, maybe a new iPad Air. You know, these days they tax you on everything, so when we go shopping, let’s not forget that we have to pay a 5% sales tax. We’ll use our array_map() function to help us there.

Ok check it out. We had our array of $items to shop for, but we needed to quickly calculate the sales tax for each item at a 5% rate. Well with array_map() we can apply a function to every single array element, and return an array with the newly processed results, in this case our $finalcost – neat!

Learn more about array_map() at

12. array_unique

You know there is a lot of blogging going on out there, and the search engines want to make sure they only index unique articles. Our little internet spider came across some articles below and put them into the $index. We need to make sure they are unique however, maybe we can use array_unique() to help us out!

Awesome! Working as designed! Our array_unique() got rid of that duplicate value in the array and returned us a new array with only unique titles. Did you spot the duplicate title in the original array?

Learn more about array_unique() at

13. array_slice

Come to think of it, rather than use that array_unique() on our $index, let’s go ahead and perform some surgery on that array and extract only the inner elements, leaving the first and last entry to their own devices.

And there you have it, by passing the array to work on, the starting offset, and the length of our extraction, we have successfully removed the middle 4 entries of our 6 element array. Bravo!

Learn more about array_slice() at

14. array_diff

Ok friends, our internet spider has been busy and it has created an updated index after some time. We’d like to examine our first index in comparison to the second and see what’s new. Let’s do it with our array_diff() function!

Awesome. Our internet spider has found 3 new articles on the internet titled, ‘Arrays with Style’, ‘PHP in the Enterprise’, and ‘Douglas Crockford declares PHP his favorite language’. By passing our updated index and original index to the array_diff() function, it returned only the new results to us.

Learn more about array_diff() at

15. array_search

My buddy was telling me about a cool article named ‘PHP in the Enterprise’, I wonder if our updated index was able to capture it yet? Let’s find out with array_search();

Well isn’t that cool? We see that it is in fact in the index at position 7, as returned from array_search()

Learn more about array_search() at

16. array_reverse

When you want to turn an array upside down or rightside up, you can use the array_reverse() function. I have added some elements to our original $array so we can get a better feel for how array_reverse() treats the array with both associative and indexed properties.

And here we see the results of applying the array_reverse() to our $array

Learn more about array_reverse() at

17. array_unshift

We have seen how easy it is to add items to the end of an array, but what if we need to prepend something to our array? Well that is where our nifty little array_unshift() comes in. Let’s see how it works by adding an interest of ‘music’ to our array:

Alrighty then! Check out that interest of music at the beginning of our array!

Maybe you’d rather prepend an associative keyed value rather than a numeric, in that case you can simply add your original array to a key value pair like so:

Well there you have it friends, an epic journey through the most widely used PHP array functions!

Thank you for reading PHP Array Functions – If you found this post helpful, Please do share using the buttons below!