Detailed explanation of the usage of PHP's array_column() function

PHPz
Release: 2023-06-27 16:18:01
Original
5412 people have browsed it

In PHP, it is often necessary to process and operate arrays. Sometimes, you need to obtain a specific column of data from a multi-dimensional array. In this case, you can use the array_column() function to achieve this. This article will introduce the usage of array_column() function in detail.

1. What is the array_column() function

array_column() is a PHP function used to obtain a specified column of data from a multi-dimensional array. This function was introduced in PHP 5.5 and supports associative arrays and indexed arrays.

Function prototype:
array_column ( array $array , mixed $column_key [, mixed $index_key = null ] ) : array

Parameter description:
$array: to be operated Multidimensional array;
$column_key: the key name or value of the column to be obtained;
$index_key: optional parameter, if this value is specified, it will be used as the key name of the returned array.

2. Usage Examples

Let’s look at some usage examples of the array_column() function.

  1. Index array

Suppose we have an index array $users, which stores some user information, and we want to get the names of all users from it. This can be achieved through the following code:

$users = array(0 => array('id'=>1, 'name'=>'Tom', 'email'=>'tom@example.com'),
               1 => array('id'=>2, 'name'=>'Jerry', 'email'=>'jerry@example.com'),
               2 => array('id'=>3, 'name'=>'Mike', 'email'=>'mike@example.com'));

$names = array_column($users, 'name');

print_r($names);
Copy after login

The output result is:

Array (
    [0] => Tom
    [1] => Jerry
    [2] => Mike
)
Copy after login
  1. Associative array

If our array is an associative array, we can Get a specified column of data in the same way.

For example, we have an associative array $students, which contains some information about students. We want to get the ages of all students:

$students = array('Tom' => array('age'=>20, 'gender'=>'male', 'city'=>'Beijing'),
                  'Jerry' => array('age'=>21, 'gender'=>'female', 'city'=>'Shanghai'),
                  'Mike' => array('age'=>19, 'gender'=>'male', 'city'=>'Guangzhou'));

$ages = array_column($students, 'age');

print_r($ages);
Copy after login

The output result is:

Array (
    [Tom] => 20
    [Jerry] => 21
    [Mike] => 19
)
Copy after login
  1. Get multiple key values

If we want to get multiple key values ​​from a multi-dimensional array, we can put the key names to be obtained in an array and pass them as the second parameter Enter the array_column() function.

For example, we have an array $temperatures that contains temperature data for multiple cities. We want to get the average temperature and maximum temperature of each city. We can do this:

$temperatures = array(
    array('city'=>'Beijing', 'average'=>20, 'highest'=>28),
    array('city'=>'Shanghai', 'average'=>23, 'highest'=>30),
    array('city'=>'Guangzhou', 'average'=>25, 'highest'=>32)
);

$infos = array_column($temperatures, array('average', 'highest'), 'city');

print_r($infos);
Copy after login

Output results Specify the key name of the returned array for:

Array (
    [Beijing] => Array (
        [average] => 20
        [highest] => 28
    )
    [Shanghai] => Array (
        [average] => 23
        [highest] => 30
    )
    [Guangzhou] => Array (
        [average] => 25
        [highest] => 32
    )
)
Copy after login

Sometimes, we want to specify the key name of the returned array when obtaining the data of the specified column. Can be specified in the third parameter of the array_column() function.

For example, in the previous example, we hope to use "average temperature" and "maximum temperature" as the key names of the returned array. We can write like this:

$temperatures = array(
    array('city'=>'Beijing', 'average'=>20, 'highest'=>28),
    array('city'=>'Shanghai', 'average'=>23, 'highest'=>30),
    array('city'=>'Guangzhou', 'average'=>25, 'highest'=>32)
);

$infos = array_column($temperatures, array('average', 'highest'), 'city');

$infos = array_map(function($v){return ['平均气温'=>$v[0], '最高气温'=>$v[1]];}, $infos);

print_r($infos);
Copy after login

The output result is:

Array (
    [Beijing] => Array (
        [平均气温] => 20
        [最高气温] => 28
    )
    [Shanghai] => Array (
        [平均气温] => 23
        [最高气温] => 30
    )
    [Guangzhou] => Array (
        [平均气温] => 25
        [最高气温] => 32
    )
)
Copy after login

3. Conclusion

The array_column() function can easily obtain a specified column of data from a multi-dimensional array, and the method of use is simple and clear. In actual development, we can make full use of this function to improve the efficiency and accuracy of array processing.

The above is the detailed content of Detailed explanation of the usage of PHP's array_column() function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template