Home > Backend Development > PHP Tutorial > array_column() function in php

array_column() function in php

PHP中文网
Release: 2023-03-16 19:44:01
Original
1859 people have browsed it

array_column() function in php

PHP Array Reference Manual

Get the last_name column from the recordset:

<?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;);
print_r($last_names);
?>
Copy after login

Output:

Array
(
  [0] => Griffin
  [1] => Smith
  [2] => Doe
)
Copy after login

Definition and usage

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

Syntax

array_column(array,column_key,index_key);
Copy after login

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:

<?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:

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

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!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template