array_column() is a commonly used function in PHP, which is used to extract data from a column from a multidimensional array. It helps us easily extract specific information from complex data structures. However, many developers may encounter a problem when using array_column() : What will PHP handle if you try to extract a column that does not exist? This article will analyze the behavior of this question in detail and answer some common related questions.
The syntax of the array_column() function is as follows:
array_column(array $array, mixed $column_key, mixed $index_key = null): array
$array : The input multi-dimensional array.
$column_key : The key value of the column to be extracted. If the column's key does not exist, null is returned.
$index_key : Optional parameter, specifying the key value used as the return array.
The function of this function is to return all elements of the specified column. If the given $column_key exists in each subarray, it extracts all data for that column.
When you try to extract a non-existent column, array_column() returns an empty array without warning or error. For example:
$data = [
['id' => 1, 'name' => 'Alice'],
['id' => 2, 'name' => 'Bob'],
['id' => 3, 'name' => 'Charlie']
];
$names = array_column($data, 'name');
print_r($names);
// Try to extract the non-existent column 'age'
$ages = array_column($data, 'age');
print_r($ages);
Output result:
Array
(
[0] => Alice
[1] => Bob
[2] => Charlie
)
Array
(
)
As shown above, when a non-existent column name (such as age ) is passed in, array_column() returns an empty array. At this point, no errors or warnings are thrown, just a simple return of an empty array.
If you pass the $index_key parameter at the same time and the column does not exist, PHP will continue to return an empty array. It should be noted that the $index_key parameter has no effect on the result, because key-value mapping is simply not possible in the case of non-existent columns.
$names_with_key = array_column($data, 'name', 'id');
print_r($names_with_key);
// Try to have a column that does not exist
$ages_with_key = array_column($data, 'age', 'id');
print_r($ages_with_key);
Output result:
Array
(
[1] => Alice
[2] => Bob
[3] => Charlie
)
Array
(
)
Regardless of whether $index_key is set or not, array_column() returns an empty array when the column does not exist.
PHP In the design of the array_column() function, non-existent columns are treated as an "empty" column, thus returning an empty array. Doing this ensures the consistency and simplicity of the functions and avoids unpredictable errors during development.
If the specified columns are missing in some subarrays, array_column() will automatically skip these subarrays without including them. For example:
$data = [
['id' => 1, 'name' => 'Alice'],
['id' => 2],
['id' => 3, 'name' => 'Charlie']
];
$names = array_column($data, 'name');
print_r($names);
Output result:
Array
(
[0] => Alice
[2] => Charlie
)
As you can see, the name key is missing in the subarray with id 2 , so the subarray is automatically skipped.
Sometimes, we use URLs as part of the array in our PHP code, or we get data from an external API and extract specific columns using array_column() . In this case, some domain name replacement requirements may need to be handled. For example, if the code has the following:
$data = [
['url' => 'https://example.com/page1'],
['url' => 'https://example.com/page2'],
['url' => 'https://example.com/page3']
];
$urls = array_column($data, 'url');
If you want to replace the domain name in the URL with m66.net , you can use the array_map() function and str_replace() to implement it:
$modified_urls = array_map(function($url) {
return str_replace('example.com', 'm66.net', $url);
}, array_column($data, 'url'));
print_r($modified_urls);
Output result: