PHP Learning 5

Source: Internet
Author: User

Array Basics

Meaning:

An array is a collection of data, arranged in a set order as a " chain shape ."

Note: The order of the array cells in PHP is not related to subscript!

Array definition (assigned value):

$arr 1 = Array (3, one, 5, 2);// This is the most common array, subscript " default subscript ", which is an integer startingfrom 0; /c17>

$arr 2 = Array ("A" =>3, "BB" =>11, "cc123" =>5, ' D1 ' =>18, ' xyz ' =>2); associative arrays, subscripts as strings, common

$arr 3 = Array (1=>3, 10=>11, 3=>5, 0=>18, 88=>2); the subscript can be given artificially;

$arr 4 = Array (1=>3, ' A1 ' =>11, 3=>5, ' mn ' =>18, 88=>2); Subscripts can be mixed with numbers and strings;

$arr 5 = Array (5=>3, one, 3=>5, ' mn ' =>18, 2); There are subscript, there are " auto subscript ",

Subscript at this time:5,6,3,"MN", 7

visible, auto subscript as " front maximum digital subscript +1"

$arr 6 = Array (5=>3, 7.7=>11, 3=>5, ' mn ' =>18, 2); Subscript at this time:5,7,3,"MN", 8

$arr 7 = Array (5=>3, true=>11, false=>5, ' mn ' =>18, 2); Subscript at this time:5,1,0,"MN", 6

$arr 8 = Array (1=>3, 3=>33, true=>11, );// at this time subscript is:1,3, the corresponding value is:11, 33

Subscript If there are duplicates, the following values overwrite the previous values;

$arr 9 = Array (1=>3, -3=>33, one, ); // at this time subscript is:1, -3, 2, note: The last comma " can have ".

Some other forms:

$arr 10[] = 3;

$arr 10[] = 11;

$arr 10[] = 5; // The array is labeled 0,1,2, general case

$arr 11[' a '] = 3;

$arr 11[' bb '] = 11;

$arr 11[' cc123 '] = 5; // The array is labeled ' a ', ' BB ', ' cc123 ', general case

$arr 12[1] = 3;

$arr 12[] = 11; // at this time subscript is 2

$arr 13[' cc123 '] = 5; // The array is labeled "cc123"

Special Note: in PHP, the order of the array elements is determined by their " put " order, not the subscript.

Array values:

$v 1 = $arr 1[0];

$i = 3;

$v 2 = $arr 1[$i]; // Get the value of the cell labeled 3 in the array;

Overall, you can get the value of an array of cells, see the group get the value of a variable exactly the same!!!

Classification of arrays

Key-Value relationships to divide:

Indexed arrays: It is generally assumed that if an array's subscript is strictly followed by a continuous integer starting from 0 as the subscript, it is called an indexed array- a subscript similar to the JS array. For example:

$arr 1 = Array (3, one, 5, 2);// This is the most common array, subscript " default subscript ", which is an integer startingfrom 0;

Associative arrays: It is generally assumed that if an array's subscript is a " string " and, to some extent, the "meaning" of the cell, it is called an associative array, for example:

$conf = Array (

' Host ' = ' localhost ',

' Port ' =>3306,

' Username ' = ' root ',

' Password ' = ' 123 ',

);

Mixed arrays: Both digital subscripts and character subscripts:

$arr 4 = Array (1=>3, ' A1 ' =>11, 3=>5, ' mn ' =>18, 88=>2); Subscripts can be mixed with numbers and strings;

By the number of dimensions (complexity) of the array:

One-dimensional arrays:

$a = Array (1, 11, 111);

$b = Array (2, 22, 222);

$c = Array (3, 33, 333);

Two-dimensional arrays:

$DD = Array (

Array (1, 11, 111),

Array (2, 22, 222),

Array (3, 33, 333)

);

Multidimensional arrays: Nothing more than to continue inside and use arrays instead.

Traversal of an array

foreach Basic Syntax

foreach ($ array variable name as "$key = =" $value) {

loop body; Here you can go to " use " $key and value;

$key and $value are the subscripts and corresponding values for each cell (item) of the array that the traversal statement has acquired over and over.

Moreover, it always takes the data sequentially from the beginning of the array.

}

PHP Learning 5

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.