PHP array learning how to create arrays by filling

青灯夜游
Release: 2023-03-11 21:42:02
Original
2376 people have browsed it

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

Copy after login

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$indexparameter 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$indexvalue and ends with the$index $number-1value. For example,$indexis 2,$numberis 4, then the array index is: 2, 3, 4, 5.

  • If 0, the array index starts from0and ends with the$number-1value. For example,$indexis 0,$numberis 4, then the array index is: 0, 1, 2, 3.

  • If it is negative, the array index is$index,0,1,2, ...,$number-2consists of. For example,$indexis -2,$numberis 4, then the array index is: -2, 0, 1, 2.

In the above example, the$indexofarray_fill(0,4,"hello")is 0 (the index starts from 0) ,$numberis 4 (the array has four elements), so the array index is: 0, 1, 2, 3; and$valueishello, Then the key values of these four elements arehello, so the output result is:

PHP array learning how to create arrays by filling

Looking at two examples:

Copy after login

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:

PHP array learning how to create arrays by filling

Copy after login

The output result is:

PHP array learning how to create arrays by filling

$valueThe parameter can not only be asingle value/string, or it can bean array, then what is created is atwo-dimensional array.

Copy after login

The output result is:

PHP array learning how to create arrays by filling

2. array_fill_keys() function

Copy after login

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$keysarray as key names and$valueas 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:

PHP array learning how to create arrays by filling# #array_fill_keys() is the same as the array_fill() function. The

$value

value can also accept an array, so what is created is atwo-dimensional array.

Copy after login
The output result is:

PHP array learning how to create arrays by fillingOkay, that’s all. If you want to know anything else, you can click this. → →

php video tutorial

Finally, I would like to recommend a free video tutorial on PHP arrays:

PHP function array array function video explanation

, come and learn!

The 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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!