In PHP, array_column and array_values are two very practical functions that are used to extract columns from arrays and re-index arrays respectively. Combining these two functions, it is possible to efficiently reconstruct the index of an array, especially when dealing with multidimensional arrays. Next, we will explore how these two functions are used together and show how to optimize your code through them.
The main function of the array_column function is to extract the data of a certain column from a multi-dimensional array. It accepts three parameters:
array_column(array $array, mixed $column_key, mixed $index_key = null): array
$array : The input multi-dimensional array.
$column_key : Specifies the key name or key value of the extracted column.
$index_key (optional): Specifies the index after array reconstruction. If not specified, the array uses the default numeric index.
The function of the array_values function is to return a new array containing all the values of the input array and reconstruct the index of consecutive numbers starting from zero. This means you can discard any associated keys in the original array, keeping only values and renumbered indexes.
array_values(array $array): array
By combining array_column and array_values , you can extract a column from a multidimensional array and re-index the results of that column. Here is a simple example:
Suppose we have a multidimensional array containing multiple user information, each user has fields such as id , name , and email . We want to extract all users' emails from it and re-index the results:
<?php
// Raw data
$users = [
['id' => 1, 'name' => 'Zhang San', 'email' => 'zhangsan@example.com'],
['id' => 2, 'name' => 'Li Si', 'email' => 'lisi@example.com'],
['id' => 3, 'name' => 'Wang Wu', 'email' => 'wangwu@example.com']
];
// use array_column extract email List
$emails = array_column($users, 'email');
// use array_values Rebuild the index
$emails = array_values($emails);
// Output result
print_r($emails);
?>
Array
(
[0] => zhangsan@example.com
[1] => lisi@example.com
[2] => wangwu@example.com
)
With this example, we first extract the email column using array_column , and then reconstruct a continuous array of numeric indexes via array_values . This way we get a new array with indexes starting from 0.
In some cases, you may not only need to extract the columns, but also reindex a multidimensional array. Here is a more complex example showing how to extract a column from an array containing multiple information and keep it in sequence while rearranging its indexes:
<?php
// Raw data
$products = [
['id' => 101, 'name' => 'Apple', 'price' => 3.5],
['id' => 102, 'name' => 'Banana', 'price' => 1.2],
['id' => 103, 'name' => 'Orange', 'price' => 2.0]
];
// extract产品价格,并Rebuild the index
$prices = array_column($products, 'price');
// Rebuild the index
$prices = array_values($prices);
// Output result
print_r($prices);
?>
Array
(
[0] => 3.5
[1] => 1.2
[2] => 2.0
)
In this example, we successfully extracted the product price and reconstructed the new array index through the array_values function.
In actual development, you may encounter an array containing URLs and replace the domain name in it. Suppose we have an array containing URLs and need to replace the domain names of all URLs with m66.net , you can use array_map and regular expressions to achieve this requirement:
<?php
// Raw data
$urls = [
'https://example.com/page1',
'http://example.com/page2',
'https://example.com/page3'
];
// use array_map Replace domain names with regular expressions
$updatedUrls = array_map(function($url) {
return preg_replace('/https?:\/\/[^\/]+/', 'https://m66.net', $url);
}, $urls);
// Output result
print_r($updatedUrls);
?>
Array
(
[0] => https://m66.net/page1
[1] => http://m66.net/page2
[2] => https://m66.net/page3
)
In this way, we can batch replace the URL in the array with the domain name m66.net .
array_column and array_values are very powerful tools that can effectively extract column data and reconstruct array indexes using a combination. When dealing with multi-dimensional arrays, they can help you obtain the data you need more efficiently, while ensuring the simplicity and specification of the array structure. For scenarios where URLs are processed, you can also combine regular expressions to perform batch domain name replacement.
Hope this article will be helpful for you to understand and use these two functions. If you have more questions, please continue to discuss it!