Home > Article > Backend Development > PHP 5.5 version update: How to use the array_column function to extract a column in a multidimensional array
PHP 5.5 version update: How to use the array_column function to extract a column in a multi-dimensional array
In PHP 5.5 version, a very practical new function array_column is introduced, which can easily extract a column from a multi-dimensional array Extract a specified column of data. When dealing with multi-dimensional arrays, we often need to extract specific data columns for further processing or display. Using the array_column function, this function can be easily implemented.
Below we will introduce how to use the array_column function and provide some sample code:
The basic syntax for using the array_column function is as follows:
array array_column ( array $array , mixed $column_key [, mixed $index_key = NULL ] )
Parameter description:
$array
: The multi-dimensional array from which data needs to be extracted; $column_key
: The key or index of the data column that needs to be extracted; $index_key
(optional): The key used as the index to extract data. Before using the array_column function, please make sure that your PHP version has been upgraded to 5.5 or above.
Example 1:
$data = array( array('id' => 1, 'name' => 'John', 'age' => 25), array('id' => 2, 'name' => 'Mary', 'age' => 28), array('id' => 3, 'name' => 'Tom', 'age' => 30), array('id' => 4, 'name' => 'Lisa', 'age' => 23) ); $names = array_column($data, 'name'); print_r($names);
Output result:
Array ( [0] => John [1] => Mary [2] => Tom [3] => Lisa )
In the above example, we have an array containing multiple personnel information, each person has a name. We use the array_column function to extract the names of all people and output them.
Example 2:
$data = array( array('id' => 1, 'name' => 'John', 'age' => 25), array('id' => 2, 'name' => 'Mary', 'age' => 28), array('id' => 3, 'name' => 'Tom', 'age' => 30), array('id' => 4, 'name' => 'Lisa', 'age' => 23) ); $ages = array_column($data, 'age', 'name'); print_r($ages);
Output result:
Array ( [John] => 25 [Mary] => 28 [Tom] => 30 [Lisa] => 23 )
In this example, we use the array_column function to extract each person's name as the index, and the corresponding age as value, and finally get an array indexed by the person's name.
As you can see, using the array_column function can very conveniently extract a specified column of data from a multi-dimensional array, reducing the workload of writing our own loop traversal for data extraction.
It should be noted that if the key of the extracted data column is the same in multiple arrays, only the last appearing value can be obtained. If you need to obtain all occurrences of the value, you can use the third parameter of the array_column function as the key of the index to ensure that each value can be obtained.
Summary:
In this article, we introduced the array_column function in PHP 5.5 version and provided some sample code for using this function. The array_column function can easily extract a specified column of data from a multi-dimensional array, and can choose to use a certain key as an index. This function simplifies our work with multi-dimensional arrays and improves the simplicity and readability of the code. If your PHP version has been upgraded to 5.5 and above, please try to use the array_column function to handle data extraction tasks in multi-dimensional arrays!
The above is the detailed content of PHP 5.5 version update: How to use the array_column function to extract a column in a multidimensional array. For more information, please follow other related articles on the PHP Chinese website!