PHP Beginner's Learning array declaration

Source: Internet
Author: User

An array is a form in which a number of variables of the same type are organized in an orderly manner in order to deal with them conveniently. The collection of these ordered, homogeneous data elements is called an array. The following describes the array declarations in PHP.

First, an overview of arrays

1. The nature of arrays: managing and manipulating a set of variables, batch processing

2. Compound type when array (multiple can be stored)

3. You can store any length of data in an array, or you can store any type of data

4. Array can complete the function of other language data structure (list, queue, Stack, collection Class)

Ii. Classification of arrays

There are multiple cells in the array, (cells are called elements)

Each element (subscript [key] and value)

The element is accessed by subscript (key) when the element is accessed by a single

1. One-dimensional arrays, two-dimensional arrays, three-dimensional arrays ... multidimensional arrays (arrays of arrays, where other arrays are stored in the array)

There are two types of arrays in 2.PHP

Indexed array: The index of an ordinal integer that is the subscript

Associative array: Is the subscript is the string as the index

Subscript (integer, string) only these two types of

Three, array of multiple declaration methods

1. Assigning a direct array element to a value declaration

If index subscript is not given, it will be indexed sequentially starting from 0

If index subscript is given, the next will increase by 1 from the largest start

If the preceding subscript appears, the assignment is to reassign the previous element

When mixing declarations, indexes and associations do not affect each other (do not affect the declaration of index subscript)

2. Using the array () function declaration

The default is an indexed array, if you specify subscripts for associative arrays and indexed arrays, use the key = = value, use "," split between multiple members

3. Use a different function declaration

(1) Indexed array

$user [0]=1; // User number    $user [1]= "Zhangsan"; // User name    $user [2]=10; // Age    $user [3]= "Nan"; // Sex    Echo ' <pre> ';    Print_r ($user);    Echo ' </pre> ';  

(2) Associative arrays

<?PHP$user["id"] = 1;$user["name"] = "Zhangsan";$user["age"] = 10;$user["Sex"];$user["age"] = 90;//Assign ValueEcho $user["Name"];//output//use array () to declare arrays$user=Array(1, "Zhangsan", Ten, "Nan");//declaring an associative array using array ()$user=Array("id" = 1, "name" = "Zhangsan", "Age", "sex" = "Nan");//declaring multidimensional Arrays (multiple records) to hold multiple user information records in a table$user=Array(    //call this line with $user[0], such as calling the name in the record, $user [0][1]    Array(1, "Zhangsan", Ten, "Nan"),//call this line with $user[1], such as calling the name in the record, $user [1][1]    Array(2, "Lisi", "NV")    );//Arrays Save multiple tables with multiple records per table$info=Array(    "User" =Array(        Array(1, "Zhangsan", Ten, "Nan"),Array(2, "Lisi", "NV")        ), "Score" =Array(        Array(1, 90, 80, 70),Array(2, 60, 40, 70)        )    );Echo $info["Score"] [1] [1];//output,?>

Learning array declaration for novice PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.