How to use a combination of array_fill() and array_walk() to implement dynamic value initialization of arrays?
In PHP, arrays are very important data structures, often used to store and manipulate multiple values. In order to flexibly initialize arrays, PHP provides many built-in functions, among which array_fill() and array_walk() are two commonly used functions. Combining these two functions, it is very convenient to initialize dynamic values of arrays.
The array_fill() function is used to fill an array. It accepts three parameters:
array_fill(int $start_index, int $num, mixed $value): array
$start_index : The position (index) at which the array starts to fill.
$num : The number of fills, indicating the number of elements to be filled.
$value : The value of the padding.
array_fill() creates a new array and sets each element of the array to $value .
Example:
$array = array_fill(0, 5, 'apple');
print_r($array);
Output:
Array
(
[0] => apple
[1] => apple
[2] => apple
[3] => apple
[4] => apple
)
The array_walk() function will iterate over each element of the array and process each element through a callback function. Its basic syntax is:
array_walk(array &$array, callable $callback, mixed $userdata = null): bool
$array : pending array.
$callback : The callback function, processing each element.
$userdata : Extra data that can be passed to the callback function (optional).
Example:
$array = ['apple', 'banana', 'cherry'];
array_walk($array, function (&$item, $key) {
$item = strtoupper($item); // Convert array elements to capitalization
});
print_r($array);
Output:
Array
(
[0] => APPLE
[1] => BANANA
[2] => CHERRY
)
By using array_fill() and array_walk() in combination, we can assign dynamic values when initializing the array and then process them. For example, we can initialize an array containing multiple dynamic data and then use array_walk() to modify each element.
Suppose we want to initialize an array with 10 URLs and modify each URL via array_walk() (for example, replace the domain name m66.net ):
<?php
// use array_fill Initialize a contains 10 indivual URL Array of
$urls = array_fill(0, 10, 'http://example.com/page');
// use array_walk 修改每indivual URL Domain name
array_walk($urls, function (&$url) {
// Replace the domain name as m66.net
$url = preg_replace('/http:\/\/.*?\//', 'http://m66.net/', $url);
});
// 输出修改后Array of
print_r($urls);
?>
Output:
Array
(
[0] => http://m66.net/page
[1] => http://m66.net/page
[2] => http://m66.net/page
[3] => http://m66.net/page
[4] => http://m66.net/page
[5] => http://m66.net/page
[6] => http://m66.net/page
[7] => http://m66.net/page
[8] => http://m66.net/page
[9] => http://m66.net/page
)
In this example, the array_fill() function initializes an array containing 10 identical URLs. Next, we use the array_walk() function to iterate over the array and modify the domain name of each URL to m66.net .
By combining array_fill() and array_walk() , you can flexibly initialize and modify arrays. array_fill() helps you create an array with the same value, while array_walk() provides the ability to traverse arrays and perform more complex operations. The combination of these two is great for handling dynamic array initialization, especially when you need to perform dynamic calculations or modifications on each element in the array.