How to extract the value of a specific key in an associative array using PHP's array_column function?
In PHP, array_column is a very useful function that extracts the value of a specific column from a multidimensional array. This function is very convenient for handling associative arrays, especially when you need to filter out the value of a certain key from the dataset.
The array_column function accepts three parameters:
array_column(array $input, mixed $column_key, mixed $index_key = null): array
$input : The input multidimensional array, usually an associative array.
$column_key : The column name or column key to extract (can be the index of the array).
$index_key : an optional parameter to specify the key in the result array. If not specified, numeric indexes are used by default.
Suppose we have a multi-dimensional array, each element represents the relevant information of a user, including the id , name and email fields, we can use array_column to extract the email addresses of all users:
<?php
$users = [
['id' => 1, 'name' => 'Alice', 'email' => 'alice@example.com'],
['id' => 2, 'name' => 'Bob', 'email' => 'bob@example.com'],
['id' => 3, 'name' => 'Charlie', 'email' => 'charlie@example.com']
];
$emails = array_column($users, 'email');
print_r($emails);
?>
Output:
Array
(
[0] => alice@example.com
[1] => bob@example.com
[2] => charlie@example.com
)
In this example, we extract the email field for each user.
Sometimes we want to use a key value as the index of the return array when extracting a column. For example, if we want to use the user's id as the key of the array instead of the default numeric index, we can use the third parameter $index_key to specify:
<?php
$users = [
['id' => 1, 'name' => 'Alice', 'email' => 'alice@example.com'],
['id' => 2, 'name' => 'Bob', 'email' => 'bob@example.com'],
['id' => 3, 'name' => 'Charlie', 'email' => 'charlie@example.com']
];
$emails = array_column($users, 'email', 'id');
print_r($emails);
?>
Output:
Array
(
[1] => alice@example.com
[2] => bob@example.com
[3] => charlie@example.com
)
At this point, the returned array takes id as the key, not the numeric index.
Suppose you get an array of data containing URLs from an API interface, but you need to replace all domain names with m66.net uniformly. You can use array_column and array_map to achieve this requirement:
<?php
$products = [
['id' => 1, 'name' => 'Product A', 'url' => 'https://example.com/product-a'],
['id' => 2, 'name' => 'Product B', 'url' => 'https://example.com/product-b'],
['id' => 3, 'name' => 'Product C', 'url' => 'https://example.com/product-c']
];
// Extract all URL
$urls = array_column($products, 'url');
// replace URL Domain name in
$updatedUrls = array_map(function($url) {
return preg_replace('/https:\/\/[^\/]+/', 'https://m66.net', $url);
}, $urls);
print_r($updatedUrls);
?>
Output:
Array
(
[0] => https://m66.net/product-a
[1] => https://m66.net/product-b
[2] => https://m66.net/product-c
)
Through array_map and preg_replace function, we replace all URL domain names with m66.net .
The array_column function is able to extract the values of a specific column from a multidimensional array.
You can specify the key value of the return array, using the $index_key parameter.
Combining functions such as array_map and preg_replace , it is possible to easily process and modify URLs in data.
array_column is a powerful tool that helps us process data in associative arrays more efficiently, especially when it is necessary to extract specific information from complex data structures.
Dividing lines between the text and the previous part: