Hướng dẫn
Quảng cáo

Hàm array_fill() trong PHP

Hướng dẫn cách sử dụng hàm array_fill() về mảng trong lập trình PHP

Tác dụng của hàm array_fill()

The array_fill() function fills an array with a specified value.

The following table summarizes the technical details of this function.

Return Value: Returns the filled array.
Changelog: Since PHP 5.6.0, the count parameter may now be zero. Previously, count was required to be greater than zero.
Version: PHP 4.2.0+

Syntax

The basic syntax of the array_fill() function is given with:

array_fill(start_index, count, value);

The following example shows the array_fill() function in action.

<?php
// Filling arrays
$array1 = array_fill(1, 5, "apple");
$array2 = array_fill(-2, 6, "banana");

// Printing the arrays
print_r($array1);
print_r($array2);
?>

Note: If you specify a negative value for the start_index parameter, the first index of the returned array will be start_index and the following indices will start from zero.


Parameters

The array_fill() function accepts the following parameters.

Parameter Description
start_index Required. Specifies the first index of the returned array.
count Required. Specifies the numbers of values to fill. Must be greater than or equal to zero.
value Required. Specifies the value to use for filling the array.

Bài viết này đã giúp ích cho bạn?

Bài viết mới

Advertisements