PHP beginner's learning array declaration. Arrays are a form of organizing several variables of the same type in order to facilitate processing in programming. The set of these sort-by-order similar data elements
ArrayIn programming, to facilitate processing, several variables of the same type are organized in an orderly manner. The collections of these sort-by-order data elements are called arrays. The following describes the array declaration in PHP.
I. Overview of arrays
1. the essence of arrays: managing and operating a group of variables, batch processing
2. array composite type (multiple objects can be stored)
3. data of any length can be stored in an array or any type of data.
4. arrays can be used to complete data structures in other languages (linked lists, queues, stacks, and collection classes)
II. array classification
An array contains multiple units (elements)
Each element (subscript [key] and value)
When accessing a single element, all elements are accessed by subscript (key ).
1. one-dimensional array, two-dimensional array, three-dimensional array... Multi-dimensional array (array, which contains other arrays)
2. PHP has two types of arrays
Index array: index where the subscript is an ordered integer
Associated array: the subscript is a string as an index.
Only these two types of subscript (integer, string)
3. multiple array declaration methods
1. directly assign a value to an array element
If the index subscript is not given, the index starts from 0.
If an index subscript is given, the next index increases by 1 from the maximum.
If the subscript appears next to it, if it is a value assignment, it is to re-assign the value to the previous element.
During hybrid declaration, the index and association do not affect each other (the index bottom object declaration is not affected)
2. use the array () function declaration
The default value is an index array. if you specify a subscript for the associated array and index array, use the key => value to separate multiple members using ","
3. use other function declarations
(1) index array
- $ User [0] = 1; // user serial number
- $ User [1] = "zhangsan"; // user name
- $ User [2] = 10; // age
- $ User [3] = "nan"; // gender
- Echo'
';
- Print_r ($ user );
- Echo'
';
(2) join an array
- <'Php
- $ User ["id"] = 1;
- $ User ["name"] = "zhangsan ";
- $ User ["age"] = 10;
- $ User ["sex"];
- $ User ["age"] = 90; // value assignment
- Echo $ user ["name"]; // output
- // Use array () to declare an array
- $ User = array (1, "zhangsan", 10, "nan ");
- // Declare the joined array using array ()
- $ User = array ("id" => 1, "name" => "zhangsan", "age" => 10, "sex" => "nan ");
- // Declare multi-dimensional arrays (multiple records) to save multiple user information records in a table
- $ User = array (
- // Call this line with $ user [0]. for example, call the name in this record, $ user [0] [1]
- Array (1, "zhangsan", 10, "nan "),
- // Call this line with $ user [1]. for example, call the name in this record, $ user [1] [1]
- Array (2, "lisi", 20, "nv ")
- );
- // Multiple tables are saved in an array. Each table has multiple records.
- $ Info = array (
- "User" => array (
- Array (1, "zhangsan", 10, "nan "),
- Array (2, "lisi", 20, "nv ")
- ),
- "Score" => array (
- Array ),
- Array (2, 60, 40, 70)
- )
- );
- Echo $ info ["score"] [1] [1]; // output 60,
- ?>
I hope this article will help you.
In programming, sort organizes several variables of the same type in an orderly manner for convenience. Set of these sort-like data elements in order...