Home >Backend Development >PHP Tutorial >PHP function introduction—array_fill(): fills the array with the specified value

PHP function introduction—array_fill(): fills the array with the specified value

WBOY
WBOYOriginal
2023-07-24 14:13:231377browse

PHP function introduction—array_fill(): Fill an array with a specified value

In PHP, there are many built-in array functions that can conveniently operate arrays. One very useful function is array_fill(). The array_fill() function can fill a new array based on the specified index range and set the value corresponding to each index to the specified value. This function is mainly used to quickly fill in some default values ​​when creating a new array.

Let us learn more about the usage of array_fill() function and some examples:

Syntax:
array_fill (int $start_index, int $num, mixed $value): array

Parameters:

  • $start_index: Required, set the starting index of the new array.
  • $num: Required, sets the length of the new array.
  • $value: Required, set the value to populate the new array.

Return value:
The function returns a new array filled with the specified value. If parameter $num is less than or equal to 0, an empty array is returned.

Example:
The following are some examples of using the array_fill() function:

//Example 1: Create an index array from 0 to 4, and fill the corresponding The value is 42
$arr ​​= array_fill(0, 5, 42);
print_r($arr);
// Output: Array ( [0] => 42 [1] => 42 [2] => 42 [3] => 42 [4] => 42 )

// Example 2: Create an index array from 3 to 7, and fill in the corresponding The value is "Hello"
$arr ​​= array_fill(3, 5, "Hello");
print_r($arr);
// Output: Array ( [3] => Hello [4] => Hello [5] => Hello [6] => Hello [7] => Hello )

// Example 3: Create an index array from -2 to 2 and fill it The value corresponding to each index is [1, 2, 3]
$arr ​​= array_fill(-2, 5, [1, 2, 3]);
print_r($arr);
// Output: Array ( [-2] => Array ( [0] => 1 [1] => 2 [2] => 3 )
// [-1] => Array ( [ 0] => 1 [1] => 2 [2] => 3 )
// [0] => Array ( [0] => 1 [1] => 2 [2 ] => 3 )
// [1] => Array ( [0] => 1 [1] => 2 [2] => 3 )
// [2] = > Array ( [0] => 1 [1] => 2 [2] => 3 ) )

// Example 4: Create an empty array and fill
$arr ​​= array_fill(0, 0, "Value");
print_r($arr);
// Output: Array ( )

In the above example, we can see the array_fill() function Create and populate arrays very easily. It allows specifying the starting index, length, and padding values ​​of the new array. Whether you're filling strings, numbers, or arrays, you can do it easily.

It should be noted that if the parameter $num is less than or equal to 0, an empty array will be returned. Therefore, before using the array_fill() function, make sure that the parameters passed in are legal.

Summary:
The array_fill() function is a very practical and convenient PHP array function. It can fill a new array according to the specified index range. By specifying the starting index, length, and padding values, you can quickly create an array with default values ​​for use in subsequent operations. The array_fill() function comes in handy when we need to create a large array and every index has the same initial value.

I hope this article can help you understand and use the array_fill() function!

The above is the detailed content of PHP function introduction—array_fill(): fills the array with the specified value. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn