In PHP, the array_column function is a very powerful tool that can help you extract data from a specific column from a multidimensional array. Usually, array_column is used to extract all values of a column from a two-dimensional array, but we can also customize the extracted content with some tricks, such as extracting the combination of column indexes and values. Next, I will explain how to achieve this.
First, let's briefly review the basic usage of the array_column function. Its syntax is as follows:
array_column(array $array, mixed $column_key, mixed $index_key = null): array
$array : The input multi-dimensional array.
$column_key : The key of the column to be extracted (can be a column name or index).
$index_key (optional): Specifies the column used as the index key to return each element in the array.
Suppose you have a two-dimensional array like this:
$people = [
['id' => 1, 'name' => 'John', 'age' => 25],
['id' => 2, 'name' => 'Jane', 'age' => 30],
['id' => 3, 'name' => 'Tom', 'age' => 35]
];
You can use array_column to extract specific columns, such as extracting name columns:
$names = array_column($people, 'name');
print_r($names);
The output result is:
Array
(
[0] => John
[1] => Jane
[2] => Tom
)
Now, if we want to extract the combination of index and value of the column, we can do it with a little modification.
To customize the combination of column indexes and values in the extract array, we can use the array_column function to extract the target column and then iterate through the column array to combine the indexes and values. For example, we can extract the name column and index it with the corresponding id , and finally output an array composed of id and name key value pairs.
$people = [
['id' => 1, 'name' => 'John', 'age' => 25],
['id' => 2, 'name' => 'Jane', 'age' => 30],
['id' => 3, 'name' => 'Tom', 'age' => 35]
];
// extract id and name Combination of columns
$result = array_column($people, 'name', 'id');
// Output result
print_r($result);
Array
(
[1] => John
[2] => Jane
[3] => Tom
)
In this example, array_column($people, 'name', 'id') extracts the name column and generates a new array with id as index. In this way, we get an array of id and name .
If we need to extract a combination of multiple columns from an array, array_column cannot directly meet the needs. At this time, it can be implemented through custom functions. For example, suppose we want to combine the id and age columns into an array with id as an index and the value is a subarray containing name and age .
$people = [
['id' => 1, 'name' => 'John', 'age' => 25],
['id' => 2, 'name' => 'Jane', 'age' => 30],
['id' => 3, 'name' => 'Tom', 'age' => 35]
];
$result = [];
foreach ($people as $person) {
$result[$person['id']] = ['name' => $person['name'], 'age' => $person['age']];
}
// Output result
print_r($result);
Array
(
[1] => Array
(
[name] => John
[age] => 25
)
[2] => Array
(
[name] => Jane
[age] => 30
)
[3] => Array
(
[name] => Tom
[age] => 35
)
)
In this example, we manually iterate over the array and build a new array where id is the key and name and age are the combination of values.
The array_column function is a very powerful tool that can extract data from a single column from a two-dimensional array. For more complex requirements, such as extracting a combination of column indexes and values or a combination of multiple columns, we can achieve it by traversing the array and customizing the processing method. By flexibly using array_column and some simple array operations, you can easily implement various data extraction and formatting operations.
Hopefully this article helps you better understand how to use the array_column function and customize extracting data from an array! If you have any questions, feel free to ask questions!