array_column

(PHP 5 >= 5.5.0, PHP 7)

array_column锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷指锟斤拷锟斤拷一锟斤拷

说锟斤拷

array_column ( array $input , mixed $column_key [, mixed $index_key = null ] ) : array

array_column() 锟斤拷锟斤拷input锟斤拷锟斤拷锟叫硷拷值为column_key锟斤拷锟叫o拷 锟斤拷锟街革拷锟斤拷丝锟窖★拷锟斤拷锟�index_key锟斤拷锟斤拷么input锟斤拷锟斤拷锟叫碉拷锟斤拷一锟叫碉拷值锟斤拷锟斤拷为锟斤拷锟斤拷锟斤拷锟斤拷锟叫讹拷应值锟侥硷拷锟斤拷

锟斤拷锟斤拷

input

锟斤拷要取锟斤拷锟斤拷锟斤拷锟叫的讹拷维锟斤拷锟介。 锟斤拷锟斤拷峁╋拷锟斤拷前锟斤拷锟揭伙拷锟斤拷锟斤拷锟斤拷锟斤拷椋伙拷锟� public 锟斤拷锟皆会被直锟斤拷取锟斤拷锟斤拷 为锟斤拷也锟斤拷取锟斤拷 private 锟斤拷 protected 锟斤拷锟皆o拷锟斤拷锟斤拷锟绞碉拷锟� __get() 锟斤拷 __isset() 魔锟斤拷锟斤拷锟斤拷锟斤拷

column_key

锟斤拷要锟斤拷锟斤拷值锟斤拷锟叫o拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷枪锟斤拷锟斤拷锟斤拷锟斤拷锟叫的硷拷锟斤拷也锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 也锟斤拷锟斤拷锟斤拷NULL锟斤拷锟斤拷时锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟介(锟斤拷锟�index_key锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟绞憋拷颍浅锟斤拷锟斤拷茫锟�

index_key

锟斤拷为锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�/锟斤拷锟斤拷锟叫o拷锟斤拷锟斤拷锟斤拷锟角革拷锟叫碉拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷值锟斤拷

锟斤拷锟斤拷值

锟接讹拷维锟斤拷锟斤拷锟叫凤拷锟截碉拷锟斤拷锟斤拷锟介。

锟斤拷锟斤拷锟斤拷志

锟芥本 说锟斤拷
7.0.0 input 锟斤拷锟斤拷锟斤拷锟节匡拷锟斤拷锟角帮拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷椤�

锟斤拷锟斤拷

Example #1 锟接斤拷锟斤拷锟斤拷锟饺★拷锟絝irst names锟斤拷

<?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);
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

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

Example #2 锟接斤拷锟斤拷锟斤拷锟斤拷锟饺★拷锟絣ast names锟叫o拷锟斤拷锟斤拷应锟斤拷id锟斤拷为锟斤拷值

<?php
// Using the $records array from Example #1
$last_names array_column($records'last_name''id');
print_r($last_names);
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

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

Example #3 username 锟斤拷锟角从讹拷锟斤拷锟饺� 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'));
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

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

Example #4 锟斤拷取 username 锟叫o拷锟接讹拷锟斤拷通锟斤拷魔锟斤拷锟斤拷锟斤拷 __get() 锟斤拷取 private 锟斤拷 "username" 锟斤拷锟皆★拷

<?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'));
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

Array
(
    [0] => Fred
    [1] => Jane
    [2] => John
)
锟斤拷锟斤拷锟斤拷峁�__isset()锟斤拷锟结返锟截匡拷锟斤拷锟介。