Most useful and common array functions in php

In this tutorial, we will learn about PHP array functions and how to use them. Those array functions are in-built in PHP. There are many array functions in PHP but in this tutorial, we will see most common and regular use function with practical examples. Before starting, let's know more about array.

What is Array?

An array is data structure that stores fixed size collection of same data type elements. Array is simple data structure where each data element can be accessed directly using its index number.

There are three types of array:

  1. Indexed Array
  2. Multi-dimensional Array
  3. Associative Array

Each type of array can be used in particular set of data or based on requirements. Like, you just want to store some numbers then you can use indexed array but if you need to store specific numbers by key name then you need to use associative array for that.

While working on actual development then you need to perform some operations on array then you can use array function which help you to built faster and effective way. For example, you need to check particular value is in array or not. Then we can use either in_array() function or loop through array and check value in every iteration.

Let's see array functions one by one with practical examples.

array() function

The array() function is used to create new array in PHP. With this function we can create any type of array like indexed array, multi dimensional array or associative array.

In the above example, we have defined three arrays as per their type. In the index array, we pass elements only. While using an associative array we have create array of pets where key is type of pet and value is name of it. In a Multidimensional array, we have passed array as element.

is_array() Function

In previous function, we have defined array. But sometime we need to check particular value is in array or not and perform operation based on result.

While using this function, It requires three parameters first one is search term, second is array and last parameter is option for case sensitive. By default case sensitive is false. Let's take example for case sensitive.

Second example will return not found because this time it will search array for user as case sensitive.

array_keys() Function

The array_keys() function returns new array containing keys of array. It will take array as required parameter. While value and strict are optional parameter. If you pass value parameter then only keys with specified value is returned. Same as if you pass strict as parameter, it will return keys by comparing values and type too. Here integer 10 and string 10 will be ignored.

Let's take another example, her we get keys of array where value is Charlie.

array_values() Function

The array_values() function will return values of array into new array. Here, we just have to pass array as parameter.

array_push() Function

The array_push() function is used to add one or more elements into array. Let's assume we have created an array and we need to add more elements after creating array then we can use this function.

array_pop() Function

The array_pop() function removes last element from array. Here, we just have to pass an array.

array_rand() Function

Sometimes we need to pick single element from array randomly then we can use array_rand() function. It will return key or multiple key based on implementations. We can use those keys to use random values.

It will print random element every time we run script. We also get multiple random keys same time by passing parameter.

array_unique() Function

The array_unique() function will fetch unique values from array. It will automatically ignore duplicate value.

Array unique function take on input as array and returns new array with unique values. Let's take example for it.

array_search() Function

The array_search() function is used for searching values into array. It will search array and return index of value if found otherwise it will return FALSE.

In above example, it will return 2. But if we search for color red then it will return 0. So basically it will return first element's index if more than one element has same value.

compact() Function

The compact() function creates an array from variables and their values. Function will automatically create array from function's parameter. It sets variable name as key and variable value as key value. Let's take example for user.

array_diff() Function

This function will compare multiple arrays and returns new array with differences. It will compare first array with second and third and returns values that are not present in others.

In output of above example it will return green, blue and yellow.

sort() Function

There are many different types of array sorting functions in PHP. Here, we see example of sort() function which will sort array into ascending order.

It will sort array into acending order and print below output.

array_map() Function

The array_map() function sends each value of an array to a custom function, and returns an array with new values generated by custom or user defined function .

In above example, It will loop through array and checks every value is grater than 70 or not. If value is greater than 70 then it will return pass otherwise fail. Please check output of above example:

array_combine() Function

This function combines array and returns new array where first array is considers as keys and second array is considered as values.

array_merge() Function

The array_merge() function merges one or more arrays into single array.

In above example, it will generate and print new array which has all colors from both array.


Here, we have checked most popular and useful array functions in PHP. There are many other function which has individual use same as those but you need these functions regularly.

Share your thoughts

Ask anything about this examples