PHP 5.5 version update: How to use the array_column function to extract a column in a multidimensional array

王林
Release: 2023-07-30 10:04:01
Original
1175 people have browsed it

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 ] )
Copy after login

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);
Copy after login

Output result:

Array (
    [0] => John
    [1] => Mary
    [2] => Tom
    [3] => Lisa
)
Copy after login

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);
Copy after login

Output result:

Array (
    [John] => 25
    [Mary] => 28
    [Tom] => 30
    [Lisa] => 23
)
Copy after login

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!