Learn array declaration for newbies in PHP_PHP Tutorial
Array is a form of organizing several variables of the same type in an orderly manner for the convenience of processing in programming. These collections of similar data elements arranged in order are called arrays. The following introduces the array declaration in PHP.
1. Overview of arrays
1. The essence of arrays: manage and operate a set of variables, batch processing
2. Arrays are composite Type (can store multiple)
3. Arrays can store data of any length and any type of data
4. Arrays can complete the functions of other language data structures (linked lists , queue, stack, collection class)
2. Classification of arrays
There are multiple units in the array, (units are called elements)
Each element (subscript [key] and value)
When accessing an element individually, the element is accessed through the subscript (key)
1. One-dimensional array, two-dimensional array , a three-dimensional array. . . Multidimensional arrays (arrays of arrays, that is, other arrays stored in the array)
2. There are two types of arrays in PHP
Index arrays: indexes whose subscripts are sequential integers
Associative array: The subscript is a string as an index
There are only two subscripts (integer, string)
3. Multiple declaration methods for arrays
1. Directly assign value statements to array elements
If the index subscript is not given, the sequential indexing will start from 0
If the index subscript is given, The next one will be incremented by 1 starting from the largest
If the previous subscript appears later, if it is an assignment, the previous element will be reassigned
In mixed declarations, index and association do not affect each other. (Does not affect the declaration of index subscripts)
2. Use the array() function to declare
The default is an index array. If you specify a subscript for an associative array and an index array, use key => value , use " , " to separate multiple members
3. Use other function declarations
(1) Index array
<ol class="dp-c"> <li class="alt"><span><span class="vars">$user</span><span>[0]=1;</span><span class="comment">//用户序号 </span><span> </span></span></li> <li> <span class="vars">$user</span><span>[1]=</span><span class="string">"zhangsan"</span><span>;</span><span class="comment">//用户名 </span><span> </span> </li> <li class="alt"> <span class="vars">$user</span><span>[2]=10;</span><span class="comment">//年龄 </span><span> </span> </li> <li> <span class="vars">$user</span><span>[3]=</span><span class="string">"nan"</span><span>;</span><span class="comment">//性别 </span><span> </span> </li> <li class="alt"> <span class="func">echo</span><span> </span><span class="string">'<pre class="brush:php;toolbar:false">'</span><span>; </span> </li> <li> <span>print_r(</span><span class="vars">$user</span><span>); </span> </li> <li class="alt"> <span class="func">echo</span><span> </span><span class="string">'';
(2) Associative array
<ol class="dp-c"> <li class="alt"><span><span><'php </span></span></li><li><span class="vars">$user</span><span>[</span><span class="string">"id"</span><span>]=1; </span></li><li class="alt"><span class="vars">$user</span><span>[</span><span class="string">"name"</span><span>]=</span><span class="string">"zhangsan"</span><span>; </span></li><li><span class="vars">$user</span><span>[</span><span class="string">"age"</span><span>]=10; </span></li><li class="alt"><span class="vars">$user</span><span>[</span><span class="string">"sex"</span><span>]; </span></li><li><span class="vars">$user</span><span>[</span><span class="string">"age"</span><span>]=90;</span><span class="comment">//赋值 </span><span> </span></li><li class="alt"><span class="func">echo</span><span> </span><span class="vars">$user</span><span>[</span><span class="string">"name"</span><span>];</span><span class="comment">//输出 </span><span> </span></li><li><span class="comment">//使用array()声明数组 </span><span> </span></li><li class="alt"><span class="vars">$user</span><span>=</span><span class="keyword">array</span><span>(1,</span><span class="string">"zhangsan"</span><span>,10,</span><span class="string">"nan"</span><span>); </span></li><li><span class="comment">//使用array()声明关联数组 </span><span> </span></li><li class="alt"><span class="vars">$user</span><span>=</span><span class="keyword">array</span><span>(</span><span class="string">"id"</span><span>=>1,</span><span class="string">"name"</span><span>=></span><span class="string">"zhangsan"</span><span>,</span><span class="string">"age"</span><span>=>10,</span><span class="string">"sex"</span><span>=></span><span class="string">"nan"</span><span>); </span></span></li> <li> <span class="comment">//声明多维数组(多条记录),来保存一个表中的多条用户信息记录 </span><span> </span> </li> <li class="alt"> <span class="vars">$user</span><span>=</span><span class="keyword">array</span><span>( </span> </li> <li> <span class="comment">//用$user[0]调用这一行,比如调用这条记录中的姓名,$user[0][1] </span><span> </span> </li> <li class="alt"> <span class="keyword">array</span><span>(1,</span><span class="string">"zhangsan"</span><span>,10,</span><span class="string">"nan"</span><span>), </span> </li> <li> <span class="comment">//用$user[1]调用这一行,比如调用这条记录中的姓名,$user[1][1] </span><span> </span> </li> <li class="alt"> <span class="keyword">array</span><span>(2,</span><span class="string">"lisi"</span><span>,20,</span><span class="string">"nv"</span><span>) </span> </li> <li><span>); </span></li> <li class="alt"> <span class="comment">//数组保存多个表,每个表有多条记录 </span><span> </span> </li> <li> <span class="vars">$info</span><span>=</span><span class="keyword">array</span><span>( </span> </li> <li class="alt"> <span class="string">"user"</span><span>=></span><span class="keyword">array</span><span>( </span> </li> <li> <span class="keyword">array</span><span>(1,</span><span class="string">"zhangsan"</span><span>,10,</span><span class="string">"nan"</span><span>), </span> </li> <li class="alt"> <span class="keyword">array</span><span>(2,</span><span class="string">"lisi"</span><span>,20,</span><span class="string">"nv"</span><span>) </span> </li> <li><span>), </span></li> <li class="alt"> <span class="string">"score"</span><span>=></span><span class="keyword">array</span><span>( </span> </li> <li> <span class="keyword">array</span><span>(1,90,80,70), </span> </li> <li class="alt"> <span class="keyword">array</span><span>(2,60,40,70) </span> </li> <li><span>) </span></li> <li class="alt"><span>); </span></li> <li> <span class="func">echo</span><span> </span><span class="vars">$info</span><span>[</span><span class="string">"score"</span><span>][1][1];</span><span class="comment">//输出60, </span><span> </span> </li> <li class="alt"><span>?> </span></li> </ol>
I hope that the introduction in this article can help you.

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
