The array_fill_keys function is used to fill the specified key of an array with the specified value. It receives two parameters: an array of key names and a padding value, returning a new array with the key names coming from the given array and the value being the given padding value.
$keys = ['a', 'b', 'c'];
$value = 0;
$result = array_fill_keys($keys, $value);
print_r($result);
Output:
Array
(
[a] => 0
[b] => 0
[c] => 0
)
In the example above, we define an array containing the key names of a , b , and c , and use array_fill_keys to set their values all to 0 .
The array_column function is used to extract the values of a column from a multidimensional array and return an array containing these values. It accepts three parameters: the original array, the key name of the column, and optional index key name.
$data = [
['id' => 1, 'name' => 'John', 'email' => 'john@m66.net'],
['id' => 2, 'name' => 'Jane', 'email' => 'jane@m66.net'],
['id' => 3, 'name' => 'Doe', 'email' => 'doe@m66.net']
];
$emails = array_column($data, 'email');
print_r($emails);
Output:
Array
(
[0] => john@m66.net
[1] => jane@m66.net
[2] => doe@m66.net
)
In this example, we extract the values of all email fields from a multi-dimensional array $data and store them in a new array $emails .
The combination of array_fill_keys and array_column is very efficient when we need to extract certain fields from a multidimensional array and use them as keys to the new array while giving them the same default values. We can first use array_column to get the required column data, and then use array_fill_keys to generate a new associative array based on this data and assign a unified value.
$data = [
['id' => 1, 'name' => 'John', 'email' => 'john@m66.net'],
['id' => 2, 'name' => 'Jane', 'email' => 'jane@m66.net'],
['id' => 3, 'name' => 'Doe', 'email' => 'doe@m66.net']
];
$names = array_column($data, 'name');
$default_value = 'N/A';
$result = array_fill_keys($names, $default_value);
print_r($result);
Output:
Array
(
[John] => N/A
[Jane] => N/A
[Doe] => N/A
)
In this example, we first extract the values of all name fields using array_column , and then use array_fill_keys to use these values as key names of the new array, all values are set to 'N/A' .
Suppose we have some user information from the database and want to use the id of these users as keys and their email as values to generate a new associative array. In this case, the combination of array_column and array_fill_keys functions is very convenient.
// Assume this is the result obtained from the database query
$db_result = [
['id' => 1, 'name' => 'John', 'email' => 'john@m66.net'],
['id' => 2, 'name' => 'Jane', 'email' => 'jane@m66.net'],
['id' => 3, 'name' => 'Doe', 'email' => 'doe@m66.net']
];
// extract id and email
$ids = array_column($db_result, 'id');
$emails = array_column($db_result, 'email');
// use id As a key,email As value
$user_emails = array_combine($ids, $emails);
print_r($user_emails);
Output:
Array
(
[1] => john@m66.net
[2] => jane@m66.net
[3] => doe@m66.net
)
In this example, we first use array_column to extract the values of the id and email fields respectively, and then merge them through array_combine into a new array with id as the key and email as the value.
Through this article, we learned that array_fill_keys and array_column are two very powerful array processing functions in PHP. array_fill_keys can fill an array based on the given key name, while array_column can extract the value of the specified column from a multidimensional array. Using these two functions together, we are able to process and reconstruct data efficiently, especially when we need to quickly fetch data and perform array conversion.
Through the demonstration of actual cases, I believe you can master the use of these two functions and apply them in actual development. If you encounter similar array processing problems during development, try these two functions, which will bring more efficiency and simplicity to your code.