In the previous article "PHP Array Learning How to Create and Initialize a One-Dimensional Array (Detailed Code Explanation)", we introduced two conventional methods of creating an array: "Directly for the array Element assignment" and "array() function
". Today we will talk about the creation of arrays and introduce two alternative methods of creating arrays. Let’s take a look.
In PHP, we can also create new arrays by filling. The main functions used are: array_fill() and array_fill_keys().
array_fill(): will create a new array by filling the key value
array_fill_keys(): will fill the key name and key Value method to create a new array
Let’s take a closer look at these two functions through code examples.
1. array_fill() function
array_fill($index, $number, $value)
Function accepted 3 non-omitable parameters$index
(starting index),$number
(number of filling elements) and$value
(key value used for filling) .
Among them, the$index
parameter supports negative values, and the array index has the following three values:
If it is a positive number, the array The index starts from the$index
value and ends with the$index $number-1
value. For example,$index
is 2,$number
is 4, then the array index is: 2, 3, 4, 5.
If 0, the array index starts from0
and ends with the$number-1
value. For example,$index
is 0,$number
is 4, then the array index is: 0, 1, 2, 3.
If it is negative, the array index is$index
,0
,1
,2
, ...,$number-2
consists of. For example,$index
is -2,$number
is 4, then the array index is: -2, 0, 1, 2.
In the above example, the$index
ofarray_fill(0,4,"hello")
is 0 (the index starts from 0) ,$number
is 4 (the array has four elements), so the array index is: 0, 1, 2, 3; and$value
ishello
, Then the key values of these four elements arehello
, so the output result is:
Looking at two examples:
Analysis: The index starts from 3 and ends at 3 4-1=6. The key values of the array are allhello
, so the output result is:
The output result is:
$value
The parameter can not only be asingle value/string, or it can bean array, then what is created is atwo-dimensional array.
The output result is:
2. array_fill_keys() function
array_fill_keys($keys,$value)
The function accepts 2 parameters that cannot be omitted$keys
(an array containing the fill key name),$value
( populate the key value).
Simply put, the array_fill_keys() function will use the elements in the$keys
array as key names and$value
as values to fill a new array.
As can be seen from the above code example: the new array has four elements, and the key names are "a
", "b
", and "c##" #", "
d"; the key values of these four elements are all "
hello", so the output result is:
# #array_fill_keys() is the same as the array_fill() function. The
$valuevalue can also accept an array, so what is created is atwo-dimensional array
.
Okay, that’s all. If you want to know anything else, you can click this. → →
php video tutorialFinally, I would like to recommend a free video tutorial on PHP arrays:
PHP function array array function video explanationThe above is the detailed content of PHP array learning how to create arrays by filling. For more information, please follow other related articles on the PHP Chinese website!