The 27 Most Useful PHP Array Functions You Need To Know!

The 27 Most Useful PHP Array Functions You Need To Know

Now that we know exactly what Arrays in PHP are and how they work, what does PHP the language offer us in terms of working with arrays? Surely we’ll need to be able to count values in them, sort arrays by their contents, convert them between strings and arrays, find unique values, search within arrays, compare arrays to other arrays and more. PHP has got you covered with over 80, that’s right count them, functions to provide any type of operation you could ever want to perform on an array. In this episode we’ll take a look at the very most useful PHP array functions so that we can hit the ground running with arrays. Let’s dig in!

Array Utility Type Functions

First off, we’ll look at some basic utility type functions for working with arrays in PHP. Things like checking to see if a variable is an array, looking inside of an array, and so on.

1. is_array(mixed $var)

You might want to know if a variable is an array. If you created it yourself, you’ll know, but what if this is a value that came from a remote api, or database, or some other means. You’ll need to check that variable to see what it is and this is how to do it. The function signature states it can take a mixed variable. That means, just pass in any old variable you want no matter what it is. If it is an array, the function will return true, otherwise it will return false.

The $dynamic variable is an array
The “number” key of the $dynamic variable is not an array
The 0 index of the $dynamic variable is an array

Cool! By using our array of things we created earlier, we can check to confirm what data types are stored in that array. We have numbers, strings, and arrays, and this code shows how to check for those scenarios.

2. in_array( mixed $needle , array $haystack [, bool $strict = FALSE ] )

Sometimes there will be instances where an array is quite large containing maybe hundreds or thousands of values. You might need to check if a certain value is in an array and with this function you can do that. The function signature requires you to pass in a needle to search for, an array as the haystack, and optionally a strict flag with will perform a value and type sensitive search. Watch this function in action now.

The best Tomatoes make the best Red Sauce!
Time to take a spin on the John Deere and get the grass cut!

3. array_unique( array $array [, int $sort_flags = SORT_STRING ] )

This is a fantastic function to use and it’s use case is to get rid of any duplicate values in the array you provide to it. Say you run a regular expression match on a piece of text and you get back hundreds of matches in the results all stored in an array. Well, we don’t need duplicates, so we can simply apply the array_unique function to get what we want. In this example we’ll take an array of strings that has many duplicates and screen them out.

Ten Steps To a Better You
Eating Spiniach – The Pros Show You How
Falling in Love with Arrays
Stock Market Secrects Jim Cramer Will Not Share
Uplifting News by ZeroHege

The array_search function is very handy for finding a value in an array and determining where it lives. That is to say, this function provides to you the index or key for the value you are searching for. Let’s see how it works here.

The answers to all of your questions is located at index 4 of the array.

5. array_reverse( array $array [, bool $preserve_keys = false ] )

It’s really easy to take an array and reverse it’s order. With this function you also have the option to keep the keys in tact or not. Let’s test it out.

0 – Racing in the Nascar Series
1 – The answers to all of your questions
2 – Eat Veggies for good health
3 – Handle the backend with PHP
4 – Design with Twitter Bootstrap
5 – How to build a website

5 – Racing in the Nascar Series
4 – The answers to all of your questions
3 – Eat Veggies for good health
2 – Handle the backend with PHP
1 – Design with Twitter Bootstrap
0 – How to build a website

6. array_map( callable $callback , array $array1 [, array $… ] )

The array_map function is pretty useful in that you can run a function on every single element in the array. You need to define a function, then that function name is given to the array_map function as the first parameter, and the second parameter is the array to iterate over. Let’s turn our array of mixed case strings into all lowercase.

how to build a website
design with twitter bootstrap
handle the backend with php
eat veggies for good health
the answers to all of your questions
racing in the nascar series

7. array_diff( array $array1 , array $array2 [, array $… ] )

The array_diff function compares one or more arrays and provides only the values present in array one, that are not in any other arrays. Let’s see how it works.

Array ( [8] => Red Hat )

Array Counting Functions

Now that we have some useful utility type array functions covered, let’s look at some array functions related to counting values or dealing with numbers. We can count the number of things in an array, return the maximum or minimum value, and all kinds of other useful hacks. Let’s see how.

8. count( mixed $array_or_countable [, int $mode = COUNT_NORMAL ] )

The best way to see how this great function works is to simply put it into action!

We have 4 items in the dynamic array, 5 vegetables, 3 tractors, and 9 tech companies. Thanks for using the count function.

9. max( array $values )

Finds the max value in the array.

10. min( array $values )

Finds the min value in the array.

11. array_rand( array $array [, int $num = 1 ] )

Returns a random value from an array. We’ll test all three of these functions in one shot.


12. array_count_values( array $array )

This is an incredible array function you can make use of. What it does is take an array with many values, and then counts the number of times each value occurs in the array. It does this by turning the original array’s values into keys in the new array, and assigns a number count for how many times that value occurred originally as the new value. An example is in order. Say we had a huge array of stock ticker symbols. In this array there are tons of entries that occur many many times. We’ll use this function to count how many times each ticker is present.

Array ( [aapl] => 3 [goog] => 2 [yhoo] => 1 [fslr] => 1 [msft] => 1 [csco] => 2 )

Array Sorting Functions

There are a plethora of functions to do all kinds of sorts on your arrays. We’ll take a quick look at the common ones now. What we can do is take a few of our arrays so far and apply the various sorts to them. The behavior will be different based on the types that are in the array and so on. The best case scenario is you simply test the function in question before trying to use it in your program. There is a special point to notice about these following four functions. Take note that the array passed in begins with an & in the function signature. What this means is that these functions are destructive. That is to say, they directly modify the array in memory, they do not create a copy of the original. This is something to keep in mind when sorting arrays.

13. sort( array &$array [, int $sort_flags = SORT_REGULAR ] )

14. asort( array &$array [, int $sort_flags = SORT_REGULAR ] )

15. rsort( array &$array [, int $sort_flags = SORT_REGULAR ] )

16. arsort( array &$array [, int $sort_flags = SORT_REGULAR ] )

Converting Between Arrays and Strings

Converting between arrays and strings happens all the time. As such, PHP provides the implode and explode functions. They are quite easy to use and work just like their names imply. implode takes in an array and glues it together as one big string while explode takes in a string and delimiter and explodes that string into pieces based on the delimiter provided. Let’s check them out.

17. implode( string $glue , array $pieces )

18. explode( string $delimiter , string $string [, int $limit ] )


General Manipulation Functions

Last up we have a handful of general array manipulation functions that you are most likely to come across in your day to day programming. Let’s take a look at a few of these now.

19. array_key_exists( mixed $key , array $array )

We can check to see if a certain key exists in our array with this handy function. This is how to use it.

Do you have a laptop? Why yes, yes I do in fact.

20. array_keys( array $array [, mixed $search_value [, bool $strict = false ]] )

This function allows us to quickly see all of the keys of the array, but not the values. Here is how to do that.

21. array_values( array $array )

What about if we want to see all the values of the array instead of the keys? How can we accomplish that? Just like this!

22. array_push( array &$array , mixed $value1 [, mixed $… ] )

Now we come into using the push, pop, unshift, and shift array functions. These deal with adding things to the end of the array, removing things from the end of the array, adding things to the begging of an array, and removing things from the beginning of an array respectively. Let’s see them in action.

23. array_pop( array &$array )

Would you like some delicious Watermelons?

24. array_unshift( array &$array , mixed $value1 [, mixed $… ] )

25. array_shift( array &$array )

Let us eat some Apple Pies together!

26. array_splice( array &$input , int $offset [, int $length [, mixed $replacement = array() ]] )

27. array_merge( array $array1 [, array $… ] )

Well friends as they say in show biz, “That’s a WRAP!” We covered a ton of ground in this episode of our Awesome PHP Tutorial Series. Hopefully you found some of the tips and tricks useful and can apply them to your own projects.