Introduction to how to use the PHP array_column() function

王林
Release: 2023-06-27 14:42:02
Original
2659 people have browsed it

In PHP, array is a very common data type that can store a set of values. When we use an array, sometimes we need to retrieve the value corresponding to a key in it. At this time, we can use the array_column() function in PHP to get the value of the specified key from the array.

This article will introduce the use of this function from the following aspects:

  1. What is the array_column() function
  2. The syntax format of the array_column() function
  3. Parameter description of array_column() function
  4. Return value of array_column() function
  5. Usage example of array_column() function
  6. What is array_column() function

In PHP, the array_column() function refers to a function that can return the value corresponding to a specified key in an array. The function of this function is to select the value corresponding to the specified key in the multi-dimensional array and return it in the form of a new array.

  1. The syntax format of array_column() function

The syntax format of array_column() function is as follows:

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

Among them, the parameters in brackets indicate optional parameters and do not have to be passed in.

  1. Parameter description of array_column() function

array_column() function has three parameters, their meanings are as follows:

  • array: required , specify the multi-dimensional array that needs to be operated;
  • column_key: required, specify the key corresponding to the return value;
  • index_key: optional, specify the key value of the returned array. The default is null, which returns a sequence array.
  1. The return value of the array_column() function

The return value of the array_column() function is a new array containing the value corresponding to the specified key. If the index_key parameter is used, this value can also appear in each row.

  1. Usage example of array_column() function

Next we use an example to demonstrate the use of array_column() function.

Suppose we have an array containing multiple user information. The structure of the array is as follows:

$user_info = array(
 array('id' => 101, 'name' => '张三', 'age' => '26' ),
 array('id' => 102, 'name' => '李四', 'age' => '27' ),
 array('id' => 103, 'name' => '王五', 'age' => '28' ),
 array('id' => 104, 'name' => '赵六', 'age' => '29' )
);
Copy after login

We now need to take out the name information of all users from the array, then we can use array_column () function to implement. The code is as follows:

$name_list = array_column($user_info,'name');
print_r($name_list);
Copy after login

Run the above code, we can get the output as follows:

Array
(
 [0] => 张三
 [1] => 李四
 [2] => 王五
 [3] => 赵六
)
Copy after login

As can be seen from the output, this function returns a new array containing the name information of all users .

In actual use, we can also obtain different results by passing in different parameters, for example:

  1. Get an array of different key values
$id_list = array_column($user_info,'id'); //获取所有用户的id信息
$age_list = array_column($user_info,'age'); //获取所有用户的年龄信息
Copy after login
  1. Get the key value of the specified row
$name_id_list = array_column($user_info,'name','id'); //将用户对应的id作为新数组的健值
print_r($name_id_list);
Copy after login

The output result is as follows:

Array
(
 [101] => 张三
 [102] => 李四
 [103] => 王五
 [104] => 赵六
)
Copy after login

Summary:

array_column() function is a very convenient Function, which can quickly obtain the value of a specified key from a multi-dimensional array and return it in a new array, greatly simplifying our array traversal and operation process. In actual development, we can use this function cleverly based on specific needs to speed up our development efficiency.

The above is the detailed content of Introduction to how to use the PHP array_column() function. For more information, please follow other related articles on the PHP Chinese website!

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