What is array_ column in php?

❮ PHP Array Reference

Example

Get column of last names from a recordset:

// An array that represents a possible record set returned from a database
$a = array(
  array(
    'id' => 5698,
    'first_name' => 'Peter',
    'last_name' => 'Griffin',
  ),
  array(
    'id' => 4767,
    'first_name' => 'Ben',
    'last_name' => 'Smith',
  ),
  array(
    'id' => 3809,
    'first_name' => 'Joe',
    'last_name' => 'Doe',
  )
);

$last_names = array_column($a, 'last_name');
print_r($last_names);
?>

Output:

Array
(
  [0] => Griffin
  [1] => Smith
  [2] => Doe
)



Definition and Usage

The array_column() function returns the values from a single column in the input array.


Syntax

array_column(array, column_key, index_key)

Parameter Values

ParameterDescription
array Required. Specifies the multi-dimensional array (record-set) to use. As of PHP 7.0, this can also be an array of objects.
column_key Required. An integer key or a string key name of the column of values to return. This parameter can also be NULL to return complete arrays (useful together with index_key to re-index the array)
index_key Optional. The column to use as the index/keys for the returned array


Technical Details

Return Value:Returns an array of values that represents a single column from the input array
PHP Version:5.5+

More Examples

Example

Get column of last names from a recordset, indexed by the "id" column:

// An array that represents a possible record set returned from a database
$a = array(
  array(
    'id' => 5698,
    'first_name' => 'Peter',
    'last_name' => 'Griffin',
  ),
  array(
    'id' => 4767,
    'first_name' => 'Ben',
    'last_name' => 'Smith',
  ),
  array(
    'id' => 3809,
    'first_name' => 'Joe',
    'last_name' => 'Doe',
  )
);

$last_names = array_column($a, 'last_name', 'id');
print_r($last_names);
?>

Output:

Array
(
  [5698] => Griffin
  [4767] => Smith
  [3809] => Doe
)



❮ PHP Array Reference




Definition and Usage

The array_column() function returns the values from a single column of the input array and identified by the column_key.

Optionally, you can pass index_key to index the values in the returned array by the values from the index_key column of the input array.

Syntax

array array_column( array $input , mixed $column_key [, mixed $index_key = NULL ] )

Parameters

Sr.NoParameter & Description
1

input (mandatory)

A multi-dimensional array or an array of objects from which to pull a column of values from.

2

column_key (mandatory)

The column of values to return. This value may be an integer key of the column you wish to retrieve, or it may be a string key name for an associative array or property name. This value can be NULL to return complete arrays or objects

3

index_key (optional)

The column to use as the index/keys for the returned array. This value may be the integer key of the column, or it may be the string key name.

Return Values

The function array_column returns an array of values representing a single column from the input array.

PHP Version

This function was first introduced in PHP Version 5.5.0. The the ability for the input parameter to be an array of objects was introduced in 7.0.0

Example

Try out following example to get the column of first names from a recordset −

 2135,
        'first_name' => 'Zara',
        'last_name' => 'Ali',
    ),
    array(
        'id' => 3245,
        'first_name' => 'Nuha',
        'last_name' => 'Mac',
    ),
    array(
        'id' => 5342,
        'first_name' => 'Shifa',
        'last_name' => 'Alam',
    ),
    array(
        'id' => 5623,
        'first_name' => 'Riya',
        'last_name' => 'Sweet',
    )
  );

 $first_names = array_column($records, 'first_name');
 print_r($first_names);
?>

This will produce following result −

Array
(
    [0] => Zara
    [1] => Nuha
    [2] => Shifa
    [3] => Riya
)

Example

Now let's try one more example to get the column of first names from a recordset but this time we will index recordset using id

 2135,
        'first_name' => 'Zara',
        'last_name' => 'Ali',
    ),
    array(
        'id' => 3245,
        'first_name' => 'Nuha',
        'last_name' => 'Mac',
    ),
    array(
        'id' => 5342,
        'first_name' => 'Shifa',
        'last_name' => 'Alam',
    ),
    array(
        'id' => 5623,
        'first_name' => 'Riya',
        'last_name' => 'Sweet',
    )
 );
 $first_names = array_column($records, 'first_name', 'id');
 print_r($first_names);
?>

This will produce following result −

Array
(
    [2135] => Zara
    [3245] => Nuha
    [5342] => Shifa
    [5623] => Riya
)

php_function_reference.htm

What is array_ column function in PHP?

The array_column() function returns the values from a single column in the input array.

What is column array?

A column matrix is a type of matrix that has only one column. The order of the column matrix is represented by m x 1, thus the rows will have single elements, arranged in a way that they represent a column of elements.

How to get a column value in PHP?

YOU CAN USE MYSQLI ALTERNATIVE OF MYSQL EASY WAY php $con=mysqli_connect("localhost","my_user","my_password","my_db"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " .

How get key of multidimensional array in PHP?

Retrieving Values: We can retrieve the value of multidimensional array using the following method:.
Using key: We can use key of the associative array to directly retrieve the data value. ... .
Using foreach loop: We can use foreach loop to retrieve value of each key associated inside the multidimensional associative array..