php5.5 new Array function array_column using _php techniques

Source: Internet
Author: User

PHP5.5 released, which added a new array function Array_column, feel good! But the lower version of PHP to use, you have to implement:
Reference Address: Https://wiki.php.net/rfc/array_column

Copy Code code 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;
}
}

Use examples
$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 ' => ' Grault ',
' 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 get the elements in a two-dimensional array (PHP 5 >= 5.5.0)

Copy Code code as follows:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.