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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106 |
If (! Function_exists ('Array _ column ')){
Function array_column ($ input, $ columnKey, $ indexKey = null ){
$ ColumnKeyIsNumber = (is_numeric ($ columnKey ))? True: false;
$ IndexKeyIsNull = (is_null ($ indexKey ))? True: false;
$ IndexKeyIsNumber = (is_numeric ($ indexKey ))? True: false;
$ Result = array ();
Foreach (array) $ input as $ key => $ row ){
If ($ columnKeyIsNumber ){
$ Tmp = array_slice ($ row, $ columnKey, 1 );
$ Tmp = (is_array ($ tmp )&&! Empty ($ tmp ))? Current ($ tmp): null;
} Else {
$ Tmp = isset ($ row [$ columnKey])? $ Row [$ columnKey]: null;
}
If (! $ IndexKeyIsNull ){
If ($ indexKeyIsNumber ){
$ Key = array_slice ($ row, $ indexKey, 1 );
$ Key = (is_array ($ key )&&! Empty ($ key ))? Current ($ key): null;
$ Key = is_null ($ key )? 0: $ key;
} Else {
$ Key = isset ($ row [$ indexKey])? $ Row [$ indexKey]: 0;
}
}
$ Result [$ key] = $ tmp;
}
Return $ result;
}
}
// Example
$ 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'
)
);
$ FirstNames = array_column ($ records, 'First _ name ');
Print_r ($ firstNames );
/*
Array
(
[0] => John
[1] => Sally
[2] => Jane
[3] => Peter
)
*/
$ Records = array (
Array (1, 'John', 'Doe '),
Array (2, 'Sally ', 'Smith '),
Array (3, 'Jane ', 'Jones ')
);
$ LastNames = array_column ($ records, 2 );
Print_r ($ lastNames );
/*
Array
(
[0] => Doe
[1] => Smith
[2] => Jones
)
*/
$ MismatchedColumns = array (
Array (
'A' => 'foo ',
'B' => 'bar ',
'E' => 'Baz'
),
Array (
'A' => 'qux ',
'C' => 'quux ',
'D' => 'corge'
),
Array (
'A' => 'graresult ',
'B' => 'garply ',
'E' => 'Waldo'
),
);
$ Foo = array_column ($ mismatchedColumns, 'A', 'B ');
Print_r ($ foo );
/*
Array
(
[Bar] => foo
[0] => qux
[Garply] => grault
)
*/ |