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().

Func 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:

  1. 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.
  2. 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.
  3. 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:

Example: 

<?php $englandTeam = array( array("firstname"=>"Ben","lastname"=>"Strokes", "DOB"=>"04/06/1991"), array("firstname"=>"Jos","lastname"=>"Buttler", "DOB"=>"08/09/1990"), array("firstname"=>"Tom","lastname"=>"Abell", "DOB"=>"05/03/1994"), array("firstname"=>"Moeen","lastname"=>"Ali", "DOB"=>"18/06/1987"), array("firstname"=>"James","lastname"=>"Anderson", "DOB"=>"30/07/1982"), array("firstname"=>"Jofra","lastname"=>"Archer ", "DOB"=>"01/04/1995"), array("firstname"=>"Jonny","lastname"=>"Bairstow ", "DOB"=>"01/04/1995"), array("firstname"=>"Stuart","lastname"=>"Broad ", "DOB"=>"24/06/1986"), );$lastNames = array_column($englandTeam, 'lastname'); print_r($lastNames); ?>

Syntax

array_column(array, column_key, index_key)

Parameter Values

ParameterOverview
arrayThis 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_keyIt 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_keyIt is optional. There is a column which should be used as an index or key in the array that is returned

Key-Value Example

The following syntax can be used to get the first name column from a recordset, which is indexed with the id column:

Example: 

<?php $englandTeam = array( array("id"=>2122,"firstname"=>"Ben","lastname"=>"Strokes", "DOB"=>"04/06/1991"), array("id"=>2123,"firstname"=>"Jos","lastname"=>"Buttler", "DOB"=>"08/09/1990"), array("id"=>2124,"firstname"=>"Tom","lastname"=>"Abell", "DOB"=>"05/03/1994"), array("id"=>2125,"firstname"=>"Moeen","lastname"=>"Ali", "DOB"=>"18/06/1987"), array("id"=>2126,"firstname"=>"James","lastname"=>"Anderson", "DOB"=>"30/07/1982"), array("id"=>2127,"firstname"=>"Jofra","lastname"=>"Archer ", "DOB"=>"01/04/1995"), array("id"=>2128,"firstname"=>"Jonny","lastname"=>"Bairstow ", "DOB"=>"01/04/1995"), array("id"=>2129,"firstname"=>"Stuart","lastname"=>"Broad ", "DOB"=>"24/06/1986"), );$dob = array_column($englandTeam, 'firstname', 'id'); print_r($dob); ?>

Example Explanation

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.

Conclusion

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.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *