array_column_recursive

思考并回答以下问题:

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
function array_column_recursive(array $haystack, $needle) {
$found = [];
array_walk_recursive($haystack, function($value, $key) use (&$found, $needle) {
if ($key == $needle)
$found[] = $value;
});
return $found;
}

$records = [
[
'id' => 2135,
'first_name' => 'John',
'last_name' => 'Doe',
'meta' => [
'registered_on' => '1/1/2019',
'more_meta' => [
'registered_on' => 'i am 3 levels deep',
]
]
],
[
'id' => 3245,
'first_name' => 'Sally',
'last_name' => 'Smith',
'meta' => [
'registered_on' => '6/1/2019'
]
],
[
'id' => 5342,
'first_name' => 'Jane',
'last_name' => 'Jones',
'meta' => [
'registered_on' => '12/1/2019'
]
],
[
'id' => 5623,
'first_name' => 'Peter',
'last_name' => 'Doe',
'meta' => [
'registered_on' => '1/1/2020'
]
]
];

$registeredOnArray = array_column_recursive($records, 'registered_on');
var_export($registeredOnArray);

结果:

1
2
3
4
5
6
7
8
9
/*
array (
0 => '1/1/2019',
1 => 'i am 3 levels deep',
2 => '6/1/2019',
3 => '12/1/2019',
4 => '1/1/2020',
)
*/
0%