Arrays are one of the most important data structures in PHP programming. They allow developers to store multiple values in a single variable, making code cleaner and more efficient. To make working with arrays even more powerful, PHP comes with a wide range of built-in array functions that help with creating, searching, merging, splitting, and manipulating arrays.
If you’re just getting started, make sure you check our beginner-friendly guide on What is an Array in PHP? before diving into array functions.
In this article, we’ll cover the most commonly used PHP array functions, explain how they work, and provide examples so you can use them effectively in your projects.
Why Use PHP Array Functions?
Manually managing arrays can be time-consuming. Instead of writing long custom code, PHP offers built-in functions to:
- Create arrays quickly
- Combine arrays into new structures
- Count values inside arrays
- Remove duplicates
- Sort arrays (ascending, descending, or custom rules)
- Search and filter values
- Select random elements
These functions improve performance, readability, and maintainability of PHP applications.
Commonly Used PHP Array Functions
Here is a detailed explanation of the most useful array functions in PHP:
1. array()
– Create an Array
Creates a new array in PHP. It can hold multiple values under one variable, supporting both indexed and associative formats.
$names = array("Ateeq", "Abbas", "Umer");
print_r($names);
2. array_change_key_case()
– Change Case of Keys
Converts all array keys to either lowercase or uppercase. Useful when you want to normalize keys for consistent access.
$student = array("Name" => "Ateeq", "Course" => "PHP");
print_r(array_change_key_case($student, CASE_LOWER));
3. array_chunk()
– Split an Array into Parts
Splits an array into smaller arrays (chunks) of a specified size. Handy when working with large datasets or paginated lists.
$numbers = range(1, 10);
print_r(array_chunk($numbers, 3));
4. array_combine()
– Combine Two Arrays
Combines two arrays into one, using the first array as keys and the second as values. Often used to map related data sets like names to scores.
$names = array("Ateeq", "Abbas", "Umer");
$scores = array(90, 85, 88);
print_r(array_combine($names, $scores));
5. array_count_values()
– Count Values
Counts all the values in an array and returns an associative array with values as keys and their frequency as values.
$values = array(1, 2, 2, 3, 3, 3);
print_r(array_count_values($values));
6. array_diff()
– Find Differences
Compares two or more arrays and returns the values present in the first array but not in the others. Good for finding differences between datasets.
$a = array("apple", "banana", "cherry");
$b = array("banana", "kiwi");
print_r(array_diff($a, $b)); // apple, cherry
7. array_diff_assoc()
– Compare Keys and Values
Finds the difference between arrays by comparing both keys and values. Returns entries from the first array that are not in the others.
8. array_diff_key()
– Compare Keys Only
Compares only the keys of arrays and returns differences. Useful when you want to focus strictly on key mismatches.
9. array_fill()
– Fill an Array with Values
Creates an array and fills it with a specified value for a given number of elements. Often used for initializing arrays.
print_r(array_fill(0, 5, "PHP"));
10. array_merge()
– Merge Arrays
Merges two or more arrays into a single array. If arrays have the same string keys, later values overwrite earlier ones.
$a = array("red", "green");
$b = array("blue", "yellow");
print_r(array_merge($a, $b));
11. array_pop()
– Remove Last Element
Removes and returns the last element of an array. Commonly used when treating arrays as stacks.
12. array_push()
– Add Elements to End
Adds one or more elements to the end of an array. It’s the opposite of array_pop()
and useful for stack operations.
13. array_product()
– Multiply All Values
Calculates and returns the product (multiplication result) of all numeric values in an array. Great for mathematical datasets.
14. array_rand()
– Random Element(s)
Returns one or more random keys from an array. Useful for sampling or selecting random values.
$names = array("Ateeq", "Abbas", "Umer", "Ali");
echo $names[array_rand($names)];
15. array_search()
– Search for a Value
Searches an array for a given value and returns the corresponding key if found. Helpful for quick lookups.
16. array_sum()
– Sum of Values
Calculates and returns the sum of all numeric values in an array. Handy for financial or statistical calculations.
17. array_unique()
– Remove Duplicates
Removes duplicate values from an array and returns the array with only unique entries.
18. array_values()
– Return All Values
Returns all the values from an array and reindexes them numerically. Useful for resetting keys after manipulation.
19. asort()
– Sort Array by Value
Sorts an array in ascending order while maintaining key associations. Often used for alphabetic or numeric sorting with preserved keys.
20. arsort()
– Sort Array in Reverse
Sorts an array in descending order while maintaining key associations. Typically used for ranking results.
21. count()
– Count Elements
Returns the number of elements in an array. A simple yet essential function to check array size.
$names = array("Ateeq", "Abbas", "Umer");
echo count($names); // Output: 3
PHP Arrays: Single vs Multidimensional
PHP supports both:
- Single-dimensional arrays (simple lists)
- Multidimensional arrays (arrays inside arrays, like tables or matrices)
This flexibility makes them powerful for handling database query results, structured data, and API responses.
Best Practices When Using Array Functions
- Use
array_unique()
to avoid duplicate entries - Combine
array_filter()
with custom functions for cleaner data - For large datasets, prefer
array_map()
over manual loops - Always check if arrays exist before applying functions (
isset
,empty
)
Conclusion
PHP arrays are essential for handling and organizing data. With the wide range of PHP array functions, developers can simplify tasks like merging, sorting, searching, and filtering data.
By mastering these functions, you can write cleaner, faster, and more efficient PHP code.
👉 Want to start from the basics? Check out What is an Array in PHP? for a beginner-friendly introduction.