Home > PHP > Most Useful PHP Array Functions

Most Useful PHP Array Functions

Hello, friends, In this tutorial, we will learn about PHP array functions and how you can use them. These array functions are in-built functions. There are many array functions but in this tutorial, we will cover the most useful and popular handy to use functions.

PHP Array Functions

The array is a way of storing data and you can say it’s like storing similar data in order

An array in PHP is actually like an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses.

As we know PHP supports Indexed arrays, Associative arrays, and Multidimensional arrays and the below-listed PHP array functions will help you to work with them.

The following list of the most useful PHP array functions.

array()

This function will create a new array and it is very useful to work with an array. You can create Indexed Array, Associative Array, and Multidimensional Array with this function.

is_array($arr)

It is used to check, provided data is array data or not. It will return true if data is in array form otherwise will return false.

in_array($search, $arr, $type)

When we need to check a specific value is exist in the array or not then we used this function to check it.

sizeof($arr)

This function will count the number of elements in an array and return the total count of the array. You can also use the count($arr) function to count the array elements.

array_merge($arr1, $arr2)

The array_merge() is used to combine the two arrays and make one single array from both of them. You can combine any two types (Indexed, Associative) of the array.

In the above example, we used the print_r() function to print the array along with key pair values.

array_keys($arr)

To grab the only keys from an array, we can use this function. It will print the all keys of the array.

array_values($arr)

It will grab the only values from an array and print them accordingly. We can create a new array to store the only values from an array with the help of this function.

array_push($arr, $val)

With the help of this function, we can add a new element at the end of an array. Suppose you have a new student and want to add in the $students array then you can use this function.

array_pop($arr)

This function works opposite of array_push() function. It will remove only one the last element from an array.

array_shift($arr)

This function used to remove the first element of an array. It is the same function as the array_pop() function but the difference is the element position to removed.

array_unshift($arr1, $value1, $value2…)

This is also a very useful function to add new elements of the array then we used this function. It will add the new elements at the beginning of the array.

array_map(‘function_name’, $arr)

Instead of using for and foreach loop in PHP, use the array_map() function to perform any function on the stored values of an array.

We need to make a function to perform the operation and use it in the array_map() function.

You can also use the array_walk($arr, 'function_name') function. Only diffenrence is the position of parameters.

array_flip($arr)

It will flips (exchange) all keys with their associative values in an array. This means, the array keys will become values and array values become keys.

array_unique($arr)

As we understand from the function name, it is used to filter the array and get rid of duplicate values from the array.

array_search($search, $arr)

The array_search() used to search the specific value from an array and it returns the key for that value you searched.

array_reverse($arr)

This function used to returns an array in the reverse order. You can preserve the keys in the same order the initially.

array_diff($arr1, $arr2, $arr3…)

It used to compare one or more arrays. It will only compare the values and return the difference of an array.

array_rand($arr)

If you want to select the random element from an array then you can use the array_rand() function. It will select the random element from an array and return it to you.

array_slice($arr, $offset, $length)

If you want to create a new subset of the array then use this function. It will return the selected parts of the array from where to start and end.

array_splice($arr, $offset, $length, $replacement)

This function is used for removes the specified element from an array and replaced it with our defined variable. It takes four parameters: An array, where to start, length from where to start, and the last one will be the replacement variable.

sort($arr)

When you want to sorts your indexed array data then use this function. It will sort your array in ascending order.

rsort($arr)

This function also uses for sorts the indexed array data. It works the same as the sort() function but it will sort the array elements in descending order.

asort($arr)

It will sort your associative array elements in ascending order and it will sort the array according to the array value.

If you want to sort the array based on the array key with ascending order then use ksort($arr) function. Check here.

arsort($arr)

It will sort your associative array elements in descending order and it will also sort the array according to the array value.

If you want to sort the array based on the array key with descending order then use krsort($arr) function. You can check here.

implode($separator, $arr)

The implode() function used to return the array elements as a string. You can also pass the separator to join each element of the array and make a string.

explode($separator, $arr)

This function will break the string into an array with the specified separator (separator is required). It will create the indexed array.

extract($arr)

This function very useful and magical. It will convert the array keys into variable names and values into variables values.

compact($var1, $var2, …)

This function is used to create an array from the variables and variables’ values. If strings do not match with a variable name then this function will be skipped that element.

array_sum($arr)

This function is also a magical function. If you want to sum all the array values then use this function. It will ignore if any string elements in an array. It will automatically identify the numbers even it is in quotes.

array_filter($arr)

This function used to filter the array values using a custom callback function with a defined operation. Each value of an array passes into a callback function to check the value against the defined condition.

So these are the most useful PHP array functions we covered in this tutorial. There are more array functions but those rarely need to use. You can check here.

If you have any queries regarding the functions or code not working, please let me know in the comment. I’ll help you with that.

Photo of author

About Aman Mehra

Hey there! I'm Aman Mehra, a full-stack developer with over six years of hands-on experience in the industry. I've dedicated myself to mastering the ins and outs of PHP, WordPress, ReactJS, NodeJS, and AWS, so you can trust me to handle your web development needs with expertise and finesse. In 2021, I decided to share my knowledge and insights with the world by starting this blog. It's been an incredible journey so far, and I've had the opportunity to learn and grow alongside my readers. Whether you're a seasoned developer or just dipping your toes into the world of web development, I'm here to provide valuable content and solutions to help you succeed. So, stick around, explore the blog, and feel free to reach out if you have any questions or suggestions. Together, let's navigate the exciting world of web development!

Leave a Comment