In PHP, we often need to construct complex multidimensional arrays. With PHP’s built-in functions array_fill() and array_merge(), we can efficiently accomplish this task. Today, let’s explore how to use these two functions to quickly build a complex multidimensional array.
array_fill() is a function used to create an array and fill each element with a specified value. This function is especially useful for creating arrays where all elements have the same value. The basic syntax is as follows:
array_fill($start_index, $num, $value);
$start_index: Specifies the starting index of the array.
$num: Specifies the number of elements in the array.
$value: The value to fill the array with.
For example, the following code creates an array of length 5, with each element set to "PHP":
$filled_array = array_fill(0, 5, 'PHP');
print_r($filled_array);
Output:
Array
(
[0] => PHP
[1] => PHP
[2] => PHP
[3] => PHP
[4] => PHP
)
array_merge() is a function used to merge one or more arrays into a single array. It combines all the elements of the arrays into a new array, returning a new array containing all elements. The basic syntax is as follows:
array_merge($array1, $array2, ...);
$array1, $array2, ...: One or more arrays to be merged.
The following code demonstrates how to use array_merge() to merge two arrays:
$array1 = [1, 2];
$array2 = [3, 4];
$merged_array = array_merge($array1, $array2);
print_r($merged_array);
Output:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
An array created with array_fill() can serve as input for array_merge() to build a more complex multidimensional array. Here’s a concrete example: let’s say we need to create a multidimensional array that contains several groups of identical data, where each group has the same structure but slightly different content.
We use array_fill() to initialize default values for each sub-array, then use array_merge() to combine them, ultimately building a complex multidimensional array.
<?php
// Use array_fill to create multiple sub-arrays with identical structure
$defaultValues = array_fill(0, 3, 'default'); // Create an array with 3 'default' values
<p>// Create different data<br>
$group1 = array_merge(['group' => 'group1'], $defaultValues);<br>
$group2 = array_merge(['group' => 'group2'], $defaultValues);<br>
$group3 = array_merge(['group' => 'group3'], $defaultValues);</p>
<p>// Merge into a multidimensional array<br>
$complexArray = [$group1, $group2, $group3];</p>
<p>print_r($complexArray);<br>
?><br>
Output:
Array
(
[0] => Array
(
[group] => group1
[0] => default
[1] => default
[2] => default
)
(
[group] => group2
[0] => default
[1] => default
[2] => default
)
[2] => Array
(
[group] => group3
[0] => default
[1] => default
[2] => default
)
)
In this example, array_fill() is used to fill each group with identical default values, and array_merge() is used to merge these into the final multidimensional array. We also added a unique identifier group1, group2, group3 to each group, making each sub-array distinct.
Let’s say we need to construct an array containing multiple URLs and replace all the domain names with m66.net. We can first use array_fill() to create an array with several default URLs, then use array_map() to process each URL and replace the domain part.
<?php
// Create an array containing multiple URLs
$urls = array_fill(0, 3, 'http://example.com/path/to/resource');
<p>// Use array_map to replace all URLs' domain with m66.net<br>
$updatedUrls = array_map(function($url) {<br>
return preg_replace('/https?://[^/]+/', '<a rel="noopener" target="_new" class="" href="http://m66.net">http://m66.net</a>', $url);<br>
}, $urls);</p>
<p>print_r($updatedUrls);<br>
?><br>
Output: