Home  >  Article  >  Backend Development  >  array_column() function in php

array_column() function in php

PHP中文网
PHP中文网Original
2017-10-27 09:21:371578browse

array_column() function in php

PHP Array Reference Manual

Get the last_name column from the recordset:

 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

array_column() Returns the value of a single column in the input array.

Syntax

array_column(array,column_key,index_key);

Parameters

Description

array Required. Specifies the multidimensional array (record set) to use.

column_key Required. The column whose value needs to be returned. Can be an integer index of a column of an index array, or a string key value of a column of an associative array. This parameter can also be NULL, in which case the entire array will be returned (very useful when used with the index_key parameter to reset the array key).

index_key Optional. The column that is the index/key of the returned array.

Technical details

Return value:

Returns an array whose value is the value of a single column in the input array.

Get the last_name column from the record set and use the corresponding "id" column as the key value:

 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
)

Complete PHP Array Reference Manual


The above is the detailed content of array_column() function in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn