= 5.5.0, PHP 7, PHP 8)array_column — 返回輸入數(shù)組中指定列的值說明array_column(array $array, int|string|null $column_key, int|string|null $index_">

array_column

(PHP 5 >= 5.5.0, PHP 7, PHP 8)

array_column返回輸入數(shù)組中指定列的值

說明

array_column(array $array, int|string|null $column_key, int|string|null $index_key = null): array

array_column() 返回 array 中鍵名為 column_key 的一列值。 如果指定了可選參數(shù) index_key,則使用輸入數(shù)組中 index_key 列的值將作為返回?cái)?shù)組中對(duì)應(yīng)值的鍵。

參數(shù)

array

多維數(shù)組或?qū)ο髷?shù)組,從中提取一列值。 如果提供的是對(duì)象數(shù)組,只有 public 的屬性會(huì)被直接取出。 如果想取出 private 和 protected 的屬性,類必須實(shí)現(xiàn) __get()__isset() 魔術(shù)方法。

column_key

需要返回值的列。它可以是索引數(shù)組的列索引,或者是關(guān)聯(lián)數(shù)組的列的鍵,也可以是屬性名。 也可以是 null ,此時(shí)將返回整個(gè)數(shù)組(配合 index_key 參數(shù)來重新索引數(shù)組時(shí)非常好用)。

index_key

作為返回?cái)?shù)組的索引/鍵的列。它可以是該列的整數(shù)索引,或者字符串鍵值。 該值會(huì)像數(shù)組鍵一樣被 強(qiáng)制轉(zhuǎn)換 (但是,在 PHP 8.0.0 之前,也被允許支持轉(zhuǎn)換為字符串對(duì)象)。

返回值

返回輸入數(shù)組中單列值的數(shù)組。

更新日志

版本 說明
8.0.0 index_key 參數(shù)指定的列中的對(duì)象不再強(qiáng)制轉(zhuǎn)換為字符串,而是會(huì)拋出 TypeError。

范例

示例 #1 從結(jié)果集中取出 first_name 列

<?php
// 表示從數(shù)據(jù)庫返回的記錄集的數(shù)組
$records = array(
    array(
        
'id' => 2135,
        
'first_name' => 'John',
        
'last_name' => 'Doe',
    ),
    array(
        
'id' => 3245,
        
'first_name' => 'Sally',
        
'last_name' => 'Smith',
    ),
    array(
        
'id' => 5342,
        
'first_name' => 'Jane',
        
'last_name' => 'Jones',
    ),
    array(
        
'id' => 5623,
        
'first_name' => 'Peter',
        
'last_name' => 'Doe',
    )
);
 
$first_names array_column($records'first_name');
print_r($first_names);
?>

以上例程會(huì)輸出:

Array
(
    [0] => John
    [1] => Sally
    [2] => Jane
    [3] => Peter
)

示例 #2 從結(jié)果集中總?cè)〕?last_name 列,用相應(yīng)的“id”作為鍵值

<?php
// 使用示例 #1 中的 $records 數(shù)組
$last_names array_column($records'last_name''id');
print_r($last_names);
?>

以上例程會(huì)輸出:

Array
(
    [2135] => Doe
    [3245] => Smith
    [5342] => Jones
    [5623] => Doe
)

示例 #3 username 列是從對(duì)象獲取 public 的 "username" 屬性

<?php

class User
{
    public 
$username;

    public function 
__construct(string $username)
    {
        
$this->username $username;
    }
}

$users = [
    new 
User('user 1'),
    new 
User('user 2'),
    new 
User('user 3'),
];

print_r(array_column($users'username'));
?>

以上例程會(huì)輸出:

Array
(
    [0] => user 1
    [1] => user 2
    [2] => user 3
)

示例 #4 通過 __get() 魔術(shù)方法從對(duì)象中獲取 private 屬性的 "name" 列。

<?php

class Person
{
    private 
$name;

    public function 
__construct(string $name)
    {
        
$this->name $name;
    }

    public function 
__get($prop)
    {
        return 
$this->$prop;
    }

    public function 
__isset($prop) : bool
    
{
        return isset(
$this->$prop);
    }
}

$people = [
    new 
Person('Fred'),
    new 
Person('Jane'),
    new 
Person('John'),
];

print_r(array_column($people'name'));
?>

以上例程會(huì)輸出:

Array
(
    [0] => Fred
    [1] => Jane
    [2] => John
)
如果不提供 __isset(),會(huì)返回空數(shù)組。