PHP array_column Function | Get Column Values From Array

The PHP array_column Function is an inbuilt function in PHP. It returns the values of a single column in a multi-dimensional array. It takes an Associative Array in input and returns an array containing column names. In this array, we will discuss the array_column function along with a few examples.

Syntax

array array_column(array, column_key, index_key);

Parameters

There are three parameters in PHP array_column Function. However, out of the three, two are mandatory parameters and one is optional. Let’s have a look at the parameters.

  1. array: The first parameter is a mandatory array. This parameter is the original array from which we need to extract the values of a single column.
  2. column_key: The second parameter is also a mandatory parameter. Column Key refers to the key of the column whose values are required. It could be both an integer or a string value. However, it may also be a NULL value to return the complete array.
  3. index_key: The third parameter is optional. Index Key refers to the column to use as an index in the returned array. It can be both a string or an integer value.

Return Type

The return type of the PHP array_column Function is an array. The array contains the values of a particular column of the input array. Optionally, the index_key may be provided to return the values in the corresponding value of the given key.

Examples

Let us go through some examples of using the PHP array_column Function.

Example 1

For instance, consider the first example.

<?php
         $testArray = array(
		array(
			'id' => 1,
			'name' => 'John',
			'email' => '[email protected]'
		),
		array(
			'id' => 5,
			'name' => 'Emma',
			'email' => '[email protected]'
		),
		array(
			'id' => 7,
			'name' => 'Chandler',
			'email' => '[email protected]'
		)
	);
        print_r(array_column($testArray, 'name'));
?>
Output  
Array
(
[0] => John
[1] => Emma
[2] => Chandler
)

In the above example, an array is created containing id, name and email of some people. In order to extract all the names in the array, we can pass name is as the column_key in the PHP array_column Function. The output of the function is an array containing only the names from the original array.

Example 2

Let’s consider the second example using all three parameters as given below:

<?php
	$testArray = array(
		array(
			'id' => 1,
			'name' => 'John',
			'email' => '[email protected]'
		),
		array(
			'id' => 5,
			'name' => 'Emma',
			'email' => '[email protected]'
		),
		array(
			'id' => 7,
			'name' => 'Chandler',
			'email' => '[email protected]'
		)
	);
        print_r(array_column($testArray, 'name', 'id'));
?>
Output
Array
(
[1] => John
[5] => Emma
[7] => Chandler
)

In the example given above, we have passed id as the index_key in the PHP array_column Function. As a result, the values in the id column of the original array serves as the indexes of the returned array.

Conclusion

In this article, we discussed PHP array_column Function. You may go through other PHP array functions on this page. Additionally, you may go through the official documentation also.

Spread the Knowledge

Leave a Reply

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