In PHP, arrays are one of the most flexible and powerful tools. Proper use of built-in functions greatly simplifies writing, reading, and maintaining code. In this article, we will discuss key functions and techniques for working with arrays, from basic to more advanced.
Basics: keys, values, pairs
PHP provides functions for:
- Creating a new array based on two others — where one is used as a list of keys and the other as a list of values.
- Retrieving only keys or only values from an associative array.
- Swapping keys and values, which can be useful when reversing logic.
- These are basic but extremely useful tools for transforming data.
Code reduction and structure simplification
PHP supports unpacking array values into variables using compact constructs. This is especially useful when working with strings or parsing arrays in a loop. Also:
You can extract variables from an associative array, automatically creating variables with names that match the keys.
Conversely, you can form an array from a set of variables, especially when you need to assemble a structure for transfer or storage.
Such approaches allow you to make the code much shorter and clearer.
Array filtering
Sometimes an array may contain:
- Empty strings,
- Zeros,
- Negative values,
- Unnecessary elements.
Filtering functions allow you to keep only the values you need, with the option to filter by value, key, or both parameters. You can also delete all “empty” values without explicit logic.
Working with unique values and columns
If an array contains duplicate data, you can easily select only the unique values. This is convenient for subsequent work, such as calculating values.
For multidimensional arrays (e.g., results from a database), you can extract only the desired column by key. This simplifies working with arrays representing entities or objects.
Iteration and transformation
Using built-in functions, you can iterate over each element of the array, performing certain actions:
- Changing the case of strings,
- Mathematical transformations,
- Formatting or merging data.
You can use both anonymous and regular functions. Some functions create a new array, while others modify the current one by reference — this should be taken into account when choosing an approach.
Merging and comparing arrays
PHP allows you to:
- Merge multiple arrays into one, while values can be overwritten if the keys match.
- Delete elements of one array from another, leaving only the difference.
- Find intersections of arrays, i.e., only those elements that are present in both.
- This is especially useful when comparing data from different sources or versions.
Mathematical operations
PHP provides functions for:
- Calculating the sum of all values,
- Multiplying elements,
- Step-by-step calculation of values based on user logic.
You can also count how many times each value occurs in the array — this is often used in analytics or statistics processing.
Generating arrays
PHP allows you to generate an array in advance:
- With a specific number of identical elements,
- With a range of values (for example, from 1 to 100, or from “a” to “z”).
- You can also cut out part of the array, for example, only the first 3 elements — to display popular entries, a limited list, etc.
Sorting arrays
Sorting in PHP can be done in different ways:
- By values or by keys,
- In ascending or descending order,
- While preserving the “key => value” relationship,
- Using custom logic (for example, sorting by string length or date).
It is important to remember that most sorting functions modify the array directly rather than creating a new one.
Combining functions: strength in simplicity
The real power lies in combining several functions. Examples:
- Remove spaces and filter empty strings — cleaning text data.
- Get id => name pairs from an array of objects — generating a drop-down list.
- Count the top 3 most frequently occurring elements.
- Calculate the total order amount from an array of products and their quantities — shopping cart calculation.
- Such combinations turn long manual code into 1–2 lines, making the project cleaner and easier to maintain.
Conclusion
Arrays are the foundation of PHP. A deep understanding of built-in functions and their correct application allows you to write concise, readable, and efficient code. It is not necessary to know all the functions by heart, but understanding their potential is a prerequisite for any PHP developer.