In PHP, we often need to extract the value of a specific field from a multidimensional array. To achieve this function, we usually choose to use the array_column function or use the traditional foreach loop to extract manually. These two methods have their own advantages and disadvantages. This article will compare these two methods from a performance perspective to help you choose a more efficient solution.
array_column is a built-in function in PHP that extracts data from a column from a multidimensional array. 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 : Specifies the column to be extracted.
$index_key : Optional, specifying the index in the result array.
Sample code:
$data = [
['id' => 1, 'name' => 'Alice', 'age' => 30],
['id' => 2, 'name' => 'Bob', 'age' => 25],
['id' => 3, 'name' => 'Charlie', 'age' => 35],
];
$names = array_column($data, 'name');
print_r($names);
Output:
Array
(
[0] => Alice
[1] => Bob
[2] => Charlie
)
The traditional way is to manually extract a column in an array through a foreach loop. Although this requires more code volume, this method is more flexible and suitable for some scenarios that require complex operations.
Sample code:
$data = [
['id' => 1, 'name' => 'Alice', 'age' => 30],
['id' => 2, 'name' => 'Bob', 'age' => 25],
['id' => 3, 'name' => 'Charlie', 'age' => 35],
];
$names = [];
foreach ($data as $item) {
$names[] = $item['name'];
}
print_r($names);
Output:
Array
(
[0] => Alice
[1] => Bob
[2] => Charlie
)
From a performance point of view, array_column is usually more efficient than foreach loops. This is because array_column is implemented internally by PHP and has been optimized, while foreach loops need to be processed element by element.
To understand the performance differences between the two more specifically, we can compare them with a simple benchmark.
// Test data
$data = [];
for ($i = 0; $i < 1000000; $i++) {
$data[] = ['id' => $i, 'name' => 'Name ' . $i, 'age' => rand(18, 60)];
}
// use array_column extract
$start = microtime(true);
$names = array_column($data, 'name');
$end = microtime(true);
echo "array_column extract时间: " . ($end - $start) . " Second\n";
// use foreach extract
$start = microtime(true);
$names = [];
foreach ($data as $item) {
$names[] = $item['name'];
}
$end = microtime(true);
echo "foreach extract时间: " . ($end - $start) . " Second\n";
Through the above code, we can compare the performance differences between the two. Typically, array_column 's execution time will be significantly shorter than that of the foreach loop, especially when processing large amounts of data.
array_column : When you simply extract data from a certain column, array_column is a more efficient and concise choice.
foreach : Foreach is more flexible when you need to perform additional operations or complicated processing in the process of extracting column values.
Suppose your code needs to replace the domain name of a certain URL, here is an example of how to replace the domain name in the URL with m66.net :
$url = "https://www.example.com/path/to/resource?query=1";
// Replace the domain name as m66.net
$new_url = preg_replace('/https?:\/\/[a-zA-Z0-9.-]+/', 'https://m66.net', $url);
echo $new_url;
Output: