PHP array learning how to create arrays by filling
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:
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 $arr=array_fill(-2,4,"hello"); var_dump($arr); ?>
The output result is:
$value
The 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:
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:
# #array_fill_keys() is the same as the array_fill() function. The
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:
Okay, that’s all. If you want to know anything else, you can click this. → →
Finally, 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!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software