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

Tutorial Básico PHP

Tutorial Avançado PHP

PHP & MySQL

Manual de Referência PHP

Uso e Exemplos da Função PHP array_column()

PHP Array Function Manual

Definição e Uso

array_column()A função retorna os valores de uma coluna única do array de entrada.

Sintaxe

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 access the input array through index_key The value of the column indexes the values in the returned array.

Parameter

Serial NumberParameters and Description
1

input (required)

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

2

column_key (required)

The column that needs to return the value. It can be the integer index of the column of the index array, or the string key value 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

index_key (optional)

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

Return Value

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

PHP Version

This function was originally introduced in PHP version5.5The version .0 introduced.7The version .0.0 introduced the feature of taking input parameters as object arrays

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