Use the new array function array_column in php5.5. PHP5.5 is released, and a new array function array_column is added. it feels good! However, if you want to use PHP in earlier versions, you have to implement it by yourself. refer to the wiki. php. netrfca PHP5.5 release. a new array function array_column is added, which is nice! However, to use PHP in earlier versions, you must implement the following:
Reference: https://wiki.php.net/rfc/array_column
The code is as follows:
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
)
*/
Array_column is used to obtain elements in a two-dimensional array (PHP 5> = 5.5.0)
The code is as follows:
// 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
) // Using the $ records array from Example #1
$ Last_names = array_column ($ records, 'Last _ name', 'id ');
Print_r ($ last_names );
?>
Array
(
[2, 2135] => Doe
[3245] => Smith
[5342] => Jones
[2, 5623] => Doe
)
Success! But the lower version of PHP to use, you have to implement: reference address: https://wiki.php.net/rfc/a...