Home  >  Article  >  Backend Development  >  Introduction to php array_column() function (example)

Introduction to php array_column() function (example)

王林
王林Original
2020-08-06 17:33:562656browse

Introduction to php array_column() function (example)

Function introduction:

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

(Recommended tutorial: php graphic tutorial)

Syntax:

array_column(array,column_key,index_key);

Parameters:

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

  • column_key Required. The column whose value needs to be returned.

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

(Video tutorial recommendation: Introduction to Programming)

Example:

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

<?php
// 可能从数据库中返回数组
$a = array(
  array(
    &#39;id&#39; => 5698,
    &#39;first_name&#39; => &#39;Peter&#39;,
    &#39;last_name&#39; => &#39;Griffin&#39;,
  ),
  array(
    &#39;id&#39; => 4767,
    &#39;first_name&#39; => &#39;Ben&#39;,
    &#39;last_name&#39; => &#39;Smith&#39;,
  ),
  array(
    &#39;id&#39; => 3809,
    &#39;first_name&#39; => &#39;Joe&#39;,
    &#39;last_name&#39; => &#39;Doe&#39;,
  )
);

$last_names = array_column($a, &#39;last_name&#39;, &#39;id&#39;);
print_r($last_names);
?>

Output result:

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

The above is the detailed content of Introduction to php array_column() function (example). 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