Analysis of the relationship between Array_map and Array_column in PHP _php skills

Source: Internet
Author: User

In this paper, the relationship between Array_map and Array_column in PHP is analyzed, and the detailed analysis is as follows:

The use of Array_map () and Array_column () is as follows:

Array_map (); function The callback function on the cell of the given array
Array_column () Fast implementation: converting two-dimensional arrays into one-dimensional arrays

The Array_column () function is in the following format:

Array Array_column (array $input, mixed $column _key [, Mixed $index _key]);

Returns the column whose value is column_key in the input array; If an optional parameter index_key is specified, the corresponding key in the returned array is the value index_key the input array value.

Sample code One:

$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);

Output:

Array
(
  [0] => John
  [1] => Sally
  [2] => Jane
  [3] => Peter
)

Example code two:

$last _names = Array_column ($records, ' last_name ', ' id ');
Print_r ($last _names);

Output:

Array
(
  [2135] => doe
  [3245] => Smith
  [5342] => Jones
  [5623] => Doe
)

When there is no array_column () function,

Use Array_map () to implement example one:

$a = Array_map (function ($element) {//$records incoming callback function as a parameter return
$element [' last_name '];            Returns the last_name corresponding value of an array element value
}, $records); Array_map returns an array, which is equivalent to storing each $element[' last_name ' in a new array, so the new index

Use foreach to implement example one:

foreach ($records as $v)
{
  $b [] = $v [' last_name '];
}
Use the Foreach implementation example two:
$c = Array ();
foreach ($records as $k => $v)
{
$c + + array ($v [' id ']=> $v [' last_name ']);//Use the + operator, in the form of an append (without altering the original array index), Merge assembled array
}//If using Array_merge, numeric key names are renumbered

In many of the data taken out, a typical two-dimensional array, if you need to use a single value in the data corresponding to the case, Array_column () can be completed, but in the face of a more complex array structure, foreach can make you more flexible, but priority to use system functions are always preferred.

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.