array_column

  1. 取某个字段形成新数组
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
$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
)

  1. 指定数组下标
1
2
3
4
5
6
7
8
9
10
11
$id_names = array_column($records, 'first_name', 'id');

print_r($id_names);

Array
(
[2135] => John
[3245] => Sally
[5342] => Jane
[5623] => Peter
)