How to create an array in php: 1. Declare the array by direct assignment, the syntax is "$array variable name [subscript] = value"; 2. Use the array() function to declare the array, the syntax is "$array variable name=array(key1=>value1,key2=>value2,...,keyN=>valueN);".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
Method 1: Direct assignment method Declaring an array
When each element in the array is a specific value rather than an array, we call such an array a one-dimensional array. One-dimensional arrays are the simplest and most commonly used arrays.
The syntax format for declaring a one-dimensional array using the direct assignment method to array elements is as follows:
$数组变量名[下标] = 值
The subscript (index value) can be a string or an integer, and the subscript Requires the [ ] package.
The sample code is as follows:
<?php header("Content-type:text/html;charset=utf-8"); $array[0] = '苹果'; $array[1] = '香蕉'; $array[2] = '橘子'; $array[3] = '榴莲'; echo '<pre class="brush:php;toolbar:false">'; var_dump($array); ?>
The running results are as follows:
array (size=4) 0 => string '苹果' (length=6) 1 => string '香蕉' (length=6) 2 => string '橘子' (length=6) 3 => string '榴莲' (length=6)
Tip: In addition to using the var_dump() function to print the entire array, you can also use the print_r() function.
There is no size limit for arrays in PHP, so in the above array, you can continue to add new elements to the array in the same way. When accessing elements in an array, you can use "$array variable name [subscript]
". The sample code is as follows:
<?php header("Content-type:text/html;charset=utf-8"); $array[0] = '苹果'; $array[1] = '香蕉'; $array[2] = '橘子'; $array[3] = '榴莲'; echo '$array[0] = ' . $array[0] . '<br>'; echo '$array[1] = ' . $array[1] . '<br>'; echo '$array[2] = ' . $array[2] . '<br>'; echo '$array[3] = ' . $array[3] . '<br>'; ?>
The running result is as follows:
$array[0] = 苹果 $array[1] = 香蕉 $array[2] = 橘子 $array[3] = 榴莲
Statement When indexing an array, if the index value is increasing, we do not need to specify a specific index value within the square brackets. In this case, the index value starts from 0 and increases sequentially by default. The sample code is as follows:
<?php header("Content-type:text/html;charset=utf-8"); $array[] = '苹果'; $array[] = '香蕉'; $array[] = '橘子'; $array[] = '榴莲'; echo '<pre class="brush:php;toolbar:false">'; var_dump($array); ?>
The running result is as follows:
array (size=4) 0 => string '苹果' (length=6) 1 => string '香蕉' (length=6) 2 => string '橘子' (length=6) 3 => string '榴莲' (length=6)
Method 2: Use the array() function to declare an array
Another way to declare an array The method is to use the array() function to create a new array. It accepts a certain number of key=>value parameter pairs separated by commas. The syntax format is as follows:
$数组变量名 = array(key1 => value1, key2 => value2, ..., keyN => valueN);
The sample code is as follows:
<?php header("Content-type:text/html;charset=utf-8"); $array = array(0 => '红色', 1 => '黄色', 2 => '蓝色', 3 => '紫色'); echo '<pre class="brush:php;toolbar:false">'; var_dump($array); ?>
The running result is as follows:
array (size=4) 0 => string '红色' (length=6) 1 => string '黄色' (length=6) 2 => string '蓝色' (length=6) 3 => string '紫色' (length=6)
If the => symbol is not used to specify the subscript, the default is the index array. The default index value also starts from 0 and increases in sequence. The sample code is as follows:
<?php header("Content-type:text/html;charset=utf-8"); $array = array('红色','黄色','蓝色', '紫色'); echo '<pre class="brush:php;toolbar:false">'; var_dump($array); ?>
The running results are the same as those of the previous example.
array (size=4) 0 => string '红色' (length=6) 1 => string '黄色' (length=6) 2 => string '蓝色' (length=6) 3 => string '紫色' (length=6)
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What are the ways to create arrays in php. 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

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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.