PHP is a very popular programming language and it is the most popular of the server-side scripting languages. PHP has a wealth of data types and structures, among which arrays are a very important data structure. In PHP, arrays can be used to store a series of related values for developers to read and operate. Let's take a look at how to write PHP arrays.
Definition of array
To define an array in PHP, you can use the array()
function or the square brackets []
. For example:
// 使用array()函数定义数组 $fruits = array("apple", "banana", "orange"); // 使用方括号[]定义数组 $colors = ["red", "green", "blue"];
The above two methods define an array containing three elements, namely "apple", "banana" and "orange" in the $fruits
array and $colors
"red", "green" and "blue" in the array. It should be noted that the elements and keys in the array can be of any variable type.
Array access
You can use the key value of the element to access the elements in the array. In PHP, array subscripts start from 0 by default. For example, to access the first element in the $fruits array we can write:
echo $fruits[0]; // 输出 "apple"
We can also use the print_r()
or var_dump()
function to print the entire Array to view the structure and contents of the array. For example:
print_r($fruits); // 输出 Array ( [0] => apple [1] => banana [2] => orange )
Use the count()
function of the array to get the number of elements in the array, for example:
echo count($fruits); // 输出 3
Addition and deletion of the array
In PHP, we can use normal assignment operators to add elements to the array, for example:
$fruits[3] = "grape"; // 添加一个元素 print_r($fruits); // 输出 Array ( [0] => apple [1] => banana [2] => orange [3] => grape )
We can also use the array_push()
function to add elements to the end of the array, for example:
array_push($fruits, "kiwi"); // 添加一个元素 print_r($fruits); // 输出 Array ( [0] => apple [1] => banana [2] => orange [3] => grape [4] => kiwi )
If you need to delete elements in the array, you can use the unset()
function. For example:
unset($fruits[2]); // 删除 orange 元素 print_r($fruits); // 输出 Array ( [0] => apple [1] => banana [3] => grape [4] => kiwi )
It should be noted that when using the unset()
function to delete array elements, the corresponding keys will also be deleted together. If you need to preserve the continuity of the keys, you can use array_values ()
Function re-indexes the array. For example:
$fruits = array_values($fruits); print_r($fruits); // 输出 Array ( [0] => apple [1] => banana [2] => grape [3] => kiwi )
Multidimensional array
In PHP, arrays can also be multidimensional and can contain any number of levels of array elements. For example:
$students = [ ["name" => "Tom", "score" => 80], ["name" => "Jerry", "score" => 90], ["name" => "Bob", "score" => 70] ];
In the above example, the $students array contains three elements, each element is an associative array containing "name" and "score" elements.
Accessing elements in a multidimensional array can be accessed through nested subscripts. For example:
echo $students[0]["name"]; // 输出 "Tom" echo $students[1]["score"]; // 输出 90
Summary
Through the above introduction, we can see the definition, access, addition, deletion of PHP arrays and the definition and access methods of multi-dimensional arrays. Mastering the basic operations of arrays can make it easier for us to organize and process data.
The above is the detailed content of How to write php array. 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

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor