首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

array_column

(PHP 5 >= 5.5.0, PHP 7)

array_column - 返回输入数组中单个列的值

描述

代码语言:javascript
复制
array array_column ( array $input , mixed $column_key [, mixed $index_key = null ] )

array_column()返回来自单个列的值input,由column_key。可选地,index_key可以提供一个通过来自index_key输入数组的列的值来对返回数组中的值进行索引。

参数

input

一个多维数组或从中抽取一列值的对象数组。如果提供了一个对象数组,则可以直接拉动公共属性。为了保护或私有属性被拉取,类必须实现__get()__isset()方法。

column_key

要返回的值的列。该值可能是您希望检索的列的整数键,也可能是关联数组或属性名称的字符串键名称。它也可能是NULL返回完整的数组或对象(这对于index_key重新索引数组非常有用)。

index_key

该列用作返回数组的index/keys。该值可能是该列的整数键,也可能是字符串键名称。

返回值

返回表示输入数组中单个列的值数组。

Changelog

版本

描述

7.0.0

增加了输入参数为对象数组的能力。

Examples

Example #1 Get the column of first names from a recordset

代码语言:javascript
复制
<?php
// Array representing a possible record set returned from a database
$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);
?>

上面的例子将输出:

代码语言:javascript
复制
Array
(
    [0] => John
    [1] => Sally
    [2] => Jane
    [3] => Peter
)

Example #2 Get the column of last names from a recordset, indexed by the "id" column

代码语言:javascript
复制
<?php
// Using the $records array from Example #1
$last_names = array_column($records, 'last_name', 'id');
print_r($last_names);
?>

上面的例子将输出:

代码语言:javascript
复制
Array
(
    [2135] => Doe
    [3245] => Smith
    [5342] => Jones
    [5623] => Doe
)

Example #3 Get the column of usernames from the public "username" property of an object

代码语言:javascript
复制
<?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'));
?>

上面的例子将输出:

代码语言:javascript
复制
Array
(
    [0] => user 1
    [1] => user 2
    [2] => user 3
)

Example #4 Get the column of names from the private "name" property of an object using the magic __get() method.

代码语言:javascript
复制
<?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'));
?>

上面的例子将输出:

代码语言:javascript
复制
Array
(
    [0] => Fred
    [1] => Jane
    [2] => John
)

如果未提供__isset(),则将返回一个空数组。

See Also

← array_chunk

array_combine →

扫码关注腾讯云开发者

领取腾讯云代金券