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

Introduction to php array_column() function (example)

王林
Release: 2023-04-09 13:06:01
Original
2720 people have browsed it

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);
Copy after login

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);
?>
Copy after login

Output result:

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template