Home  >  Article  >  Backend Development  >  PHP array learning how to create arrays by filling

PHP array learning how to create arrays by filling

青灯夜游
青灯夜游Original
2021-08-04 13:41:072384browse

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

<?php
$arr=array_fill(0,4,"hello");
var_dump($arr);
?>

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 from 0 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 of array_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 is hello, Then the key values ​​of these four elements are hello, so the output result is:

PHP array learning how to create arrays by filling

Looking at two examples:

<?php
$arr=array_fill(3,4,"hello");
var_dump($arr);
?>

Analysis: The index starts from 3 and ends at 3 4-1=6. The key values ​​of the array are all hello, so the output result is:

PHP array learning how to create arrays by filling

<?php
$arr=array_fill(-2,4,"hello");
var_dump($arr);
?>

The output result is:

PHP array learning how to create arrays by filling

$valueThe parameter can not only be a single value/string , or it can be an array, then what is created is a two-dimensional array.

<?php
header("Content-type:text/html;charset=utf-8");
$arr=array_fill(0,4,array("张三",25,"男"));
var_dump($arr);
?>

The output result is:

PHP array learning how to create arrays by filling

2. array_fill_keys() function

<?php
$keys=array("a","b","c","d");
$arr=array_fill_keys($keys,"hello");
var_dump($arr);
?>

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:

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 a two-dimensional array.

<?php
header("Content-type:text/html;charset=utf-8");
$keys=array("a","b","c","d");
$arr=array_fill_keys($keys,array("李四",21,"男"));
var_dump($arr);
?>
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!

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