PHP array_column() Function
We are talking about Func array column, If you are a PHP developer, you must be familiar with arrays, one of the most important data structures in PHP.
Arrays are used to store a collection of values in a single variable, and they can be manipulated in many ways.
One of the useful functions in PHP for manipulating arrays is array_column().
What is array_column() function?
The array_column() function / Func array column is a built-in PHP function that is used to extract a single column or key from an array of arrays. It takes three arguments:
- The first argument is the input array, which is an array of arrays or objects. Each element in this array must itself be an array or object that has the key or property to extract.
- The second argument is the column or key that you want to extract from each element in the input array. This can be either a string or an integer.
- The third (optional) argument is the key or property that you want to use as the index of the resulting array. This can also be either a string or an integer in Func array column.
How to use array_column() function?
To use the Func array column OR array_column() function, you need to pass the input array, column/key, and optionally the index key as arguments. Here’s an example:
Suppose we have an array of employees with their names, ages, and salaries. We want to extract only their salaries and store them in a separate array.
Here’s how we can do it using array_column() function:
array_column(array, column_key, index_key)
|array||This is required. Provides a way to specify a multidimensional array (record-set) that will be used. PHP 7.0 allows objects to be arrays in Func array column.|
|column_key||It is required. You can specify the column name of the values that will be returned, either as a string key or as an integer key. There is also an option of returning complete arrays with this parameter NULL (useful together with index_key if you want to reorder the arrays).|
|index_key||It is optional. There is a column which should be used as an index or key in the array that is returned|
The following syntax can be used to get the first name column from a recordset, which is indexed with the id column:
In this PHP code example, we are working with an array of information for the England cricket team. The array is stored in a variable called “$englandTeam”.
The array is a multidimensional array, where each sub-array represents a player in the team. Each sub-array contains information about the player, such as their ID, first name, last name, and date of birth when it comes to Func Array Column.
We then use the built-in PHP function “array_column()” to extract a specific column of data from the array. In this case, we extract the “firstname” column and use the “id” column as the key for the new array. We store the result in a variable called “$dob”.
We then print out the contents of the “$dob” array using the “print_r()” function.
As a result, we get an associative array where the keys are player IDs and the values are their first names. This can be useful in various scenarios, such as searching for players by their ID or displaying player names with their corresponding IDs.
In conclusion, the Func array column / array_column() function is a useful tool for PHP developers to extract a single column or key from an array of arrays or objects.
This function can help you to make your code more efficient and readable, as it simplifies the process of extracting data from arrays.
If you haven’t used this function before, give it a try in your next PHP project, and see how it can help you to write cleaner and more concise code.
If you liked this article and found it informative, you can subscribe to our newsletter below.