In PHP, processing and extracting array data is a very common task. Array manipulation functions array_column and array_map provide efficient ways to extract and manipulate multiple columns in an array, especially when dealing with two-dimensional arrays. This article will introduce how to use these two functions to efficiently extract multiple columns of data in an array and show related code examples.
array_column is a function used to extract data from a specified column from a two-dimensional array. It extracts the value corresponding to the specified key from each element in the array (usually an associative array) and returns a new array.
grammar:
array_column(array $array, $column_key, $index_key = null)
$array : The input multi-dimensional array.
$column_key : Specifies the column to be extracted.
$index_key : Optional parameter to specify a key value as the index of the new array.
Example:
Suppose we have an array of user information, each user contains id , name , and email .
$users = [
['id' => 1, 'name' => 'Alice', 'email' => 'alice@m66.net'],
['id' => 2, 'name' => 'Bob', 'email' => 'bob@m66.net'],
['id' => 3, 'name' => 'Charlie', 'email' => 'charlie@m66.net']
];
// Extract all "name" List
$names = array_column($users, 'name');
print_r($names);
Output:
Array
(
[0] => Alice
[1] => Bob
[2] => Charlie
)
Through array_column , we extract the data of the name column.
array_map can be used to apply a callback function to each element in an array to generate a new array. We can use this feature to extract data from multiple columns.
grammar:
array_map(callable $callback, array $array)
$callback : A callback function that processes each element in the array.
$array : The input array.
Example:
Suppose we now want to extract not only the name column, but also the email column, we can do this using array_map in conjunction with the callback function.
$users = [
['id' => 1, 'name' => 'Alice', 'email' => 'alice@m66.net'],
['id' => 2, 'name' => 'Bob', 'email' => 'bob@m66.net'],
['id' => 3, 'name' => 'Charlie', 'email' => 'charlie@m66.net']
];
// extract "name" and "email" 两List
$namesAndEmails = array_map(function($user) {
return [$user['name'], $user['email']];
}, $users);
print_r($namesAndEmails);
Output:
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, array_map is used to extract the two fields name and email from each user's array, generating a new two-dimensional array.
Sometimes, we want to extract data from multiple columns and process these data. This goal can be achieved efficiently by combining array_column and array_map . For example, we can extract the name and email columns of all users and replace the m66.net domain name in the email with example.com .
$users = [
['id' => 1, 'name' => 'Alice', 'email' => 'alice@m66.net'],
['id' => 2, 'name' => 'Bob', 'email' => 'bob@m66.net'],
['id' => 3, 'name' => 'Charlie', 'email' => 'charlie@m66.net']
];
// use array_map extract name and email,And modify email
$namesAndEmails = array_map(function($user) {
$user['email'] = str_replace('@m66.net', '@example.com', $user['email']);
return [$user['name'], $user['email']];
}, $users);
print_r($namesAndEmails);
Output:
Array
(
[0] => Array
(
[0] => Alice
[1] => alice@example.com
)
[1] => Array
(
[0] => Bob
[1] => bob@example.com
)
[2] => Array
(
[0] => Charlie
[1] => charlie@example.com
)
)
In this example, we not only extract the user's name and email , but also modify the email domain name from m66.net to example.com via str_replace .
array_column is a very efficient tool for extracting single column data from multidimensional arrays and is suitable for simple column extraction.
array_map provides more powerful features that can be used to perform complex processing of each element, thereby extracting data from multiple columns.
Using these two functions can effectively extract and process multi-column data in multi-dimensional arrays, avoid using nested foreach loops, and improve code readability and execution efficiency.
By using these two functions reasonably, you can greatly simplify the complexity of array data processing and improve the execution efficiency of code.