English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP array_column() Function Usage and Example

PHP Array Function Manual

Definition and Usage

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

Syntax

array array_column(array $input, mixed $column_key[, mixed $index_key = NULL])
  • column_key as the column name to be returned.

  • (optional) you can also choose to pass index_key, so that you can go through the input array's index_key The value of the column indexes the values in the returned array.

Parameter

Serial NumberParameters and Description
1

(required)

Multidimensional array or object array, from which a column of values can be extracted.

2

(required)

The column that needs to return the value. It can be an integer index of the column of the index array, or a string key value of the column of the associated array. This parameter can also be NULL, in which case the entire array will be returned (very useful when resetting the array keys with the index_key parameter).

3

(optional)

Used as the index of the returned array/The column of keys. This value can be an integer key of the column, or a string key name.

Return Value

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

PHP Version

This function was originally introduced in PHP version5.5version introduced.7version introduced the feature of taking input parameters as an object array

Online Example

Try the following example to get the names column from the record set-

<?php
 $records = array(
    array(
        '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');
 print_r($first_names);
?>
Test and see‹/›

Output Result

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

Online Example

Now let's try another example to get the 'first_name' column from the record set and useidIndex Record Set-

<?php
 $records = array(
    array(
        '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);
?>
Test and see‹/›

Output Result

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

PHP Array Function Manual