Home > Article > Backend Development > Introduction to how to use the PHP array_column() function
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:
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.
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.
array_column() function has three parameters, their meanings are as follows:
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.
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' ) );
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);
Run the above code, we can get the output as follows:
Array ( [0] => 张三 [1] => 李四 [2] => 王五 [3] => 赵六 )
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:
$id_list = array_column($user_info,'id'); //获取所有用户的id信息 $age_list = array_column($user_info,'age'); //获取所有用户的年龄信息
$name_id_list = array_column($user_info,'name','id'); //将用户对应的id作为新数组的健值 print_r($name_id_list);
The output result is as follows:
Array ( [101] => 张三 [102] => 李四 [103] => 王五 [104] => 赵六 )
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!