In PHP, the array_column function can easily extract specific column data from a multidimensional array. It is often used to extract a specified column from an associative array or multidimensional array for subsequent processing. Next, we will dive into how to use the array_column function to extract multi-column data from a multi-dimensional array.
The basic syntax of the array_column function is as follows:
array_column(array $array, $column_key, $index_key = null): array
$array : The input multi-dimensional array.
$column_key : The key name of the column that needs to be extracted.
$index_key : (optional) is used as the key to return the array. If not provided, the returned array will be an indexed array.
First, let's look at a simple example showing how to extract single column data from a multidimensional array:
<?php
$data = [
['id' => 1, 'name' => 'Alice', 'email' => 'alice@m66.net'],
['id' => 2, 'name' => 'Bob', 'email' => 'bob@m66.net'],
['id' => 3, 'name' => 'Charlie', 'email' => 'charlie@m66.net'],
];
$emails = array_column($data, 'email');
print_r($emails);
?>
Output result:
Array
(
[0] => alice@m66.net
[1] => bob@m66.net
[2] => charlie@m66.net
)
In this example, we use array_column to extract the email column.
array_column can only extract one column of data, but if you want to extract multiple columns of data, you can use the combination of array_column and array_map to achieve it. Here is an example showing how to extract multiple columns:
<?php
$data = [
['id' => 1, 'name' => 'Alice', 'email' => 'alice@m66.net'],
['id' => 2, 'name' => 'Bob', 'email' => 'bob@m66.net'],
['id' => 3, 'name' => 'Charlie', 'email' => 'charlie@m66.net'],
];
$namesAndEmails = array_map(function($item) {
return [
'name' => $item['name'],
'email' => $item['email'],
];
}, $data);
print_r($namesAndEmails);
?>
Output result:
Array
(
[0] => Array
(
[name] => Alice
[email] => alice@m66.net
)
[1] => Array
(
[name] => Bob
[email] => bob@m66.net
)
[2] => Array
(
[name] => Charlie
[email] => charlie@m66.net
)
)
In this example, we use the array_map function to extract the name and email columns into a new array.
If you want to extract multiple column data through array_column , you can usually use it in conjunction with array_column with some other functions. For example, if you only care about one column and need other data for correlation operations, here is a more complex example.
<?php
$data = [
['id' => 1, 'name' => 'Alice', 'email' => 'alice@m66.net'],
['id' => 2, 'name' => 'Bob', 'email' => 'bob@m66.net'],
['id' => 3, 'name' => 'Charlie', 'email' => 'charlie@m66.net'],
];
$names = array_column($data, 'name');
$emails = array_column($data, 'email');
// Merge two arrays
$merged = array_map(null, $names, $emails);
print_r($merged);
?>
Output result:
Array
(
[0] => Array
(
[0] => Alice
[1] => alice@m66.net
)
[1] => Array
(
[0] => Bob
[1] => bob@m66.net
)
[2] => Array
(
[0] => Charlie
[1] => charlie@m66.net
)
)
In this example, we use array_column to extract the name and email columns, and then merge them into a two-dimensional array through the array_map function.
array_column is a very powerful tool that can help you easily extract a column of data in an array.
For multiple column extraction, although array_column can only handle single columns, you can realize the requirement of extracting multiple columns by combining other functions such as array_map .
array_column is more efficient, especially when dealing with large-scale data, which is more efficient than manual loops.
By using array_column and other array functions reasonably, you can process multidimensional array data in PHP more efficiently.