PHP Learning note--7. Arrays

Source: Internet
Author: User
Tags constant definition

1. Definition of arrays

A number of data that you want to be relevant, and that you don't want to define as multiple variables, you can define an array

One: The number of the array is unique and is used to differentiate the unit

Number = = Key/key (associative array), index/index (indexed array), subscript (commonly known)

Two: stored content is called value/value

2. Declaration of an array

Array even if the collection of key/value pairs

Method One: Initialize mode

$arr = null;

$arr [' key 1 '] = ' value 1 ';

$arr [' key 2 '] = ' value 2 ';

...

Method Two: Array structure mode

$arr = Array (' key1 ' = ' value1 ', ' key2 ' = ' value ' ...);

Method Three: Automatically assign the index way

$arr = Array (' value1 ', ' value2 ' ...);

PHP automatically assigns index values starting from 0

3. How to write the subscript of the array

$arr = Array (1=> ', ' name ' = ' Zhang San ');

echo $arr [1], $arr [' 1 '], $arr ["1"]; = Medium Medium

Although all get the result, but need to put the string of ' 1 ', "1" first converted,

In finding the unit, it affects the efficiency

Therefore, the subscript of the number is not required to add single/double quotation marks

echo $arr [name], $arr [' name '], $arr ["name"]; = Zhang Sanzhang

Without single/double quotes, PHP will prioritize it as a constant parsing

If you do not have this constant, parse it to a string

Therefore, the string subscript requires a single/double quotation mark to avoid errors caused by the constant definition

Also because single quotation marks are faster than double quotation marks, so precedence plus single quotes

4. Types of array cell values

All eight types are available, that is, the value of the array is still an array (called a two-dimensional/multidimensional array)

Use array [' key '] [' key ']; Can find values in a two-dimensional array

5. Iterating the array

foreach (array variable as [key variable =>] Value variable) {

...

}

Iterate through each cell of the array, assign the key to the key variable, assign the value of the unit to the value variable

6. Cursor manipulation of arrays

Current (array variable); = = Remove the pointer to the value of the cell; pointer initialization to the NO. 0 cell

Key (array variable); = = Remove the key pointing to the cell

Next (array variable); = = Move the pointer to the tail one

Prev (array variable); = = Move the pointer to the head one

End (array variable); = = Points The pointer over the last cell

Reset (array variable); + = Resets the pointer to the NO. 0 cell

To perform an array traversal with a for mate cursor operation:

for (; current ($arr); next ($arr)) {

echo Key ($arr), ': ', current ($arr);

}

When the value of the current ($arr) condition is "", 0,false, ' 0 ', NULL, the condition is false, the loop exits

7.list and each

Each (array variable); = = The information that points to the current cell consists of an array, and the pointer moves to the tail one

The resulting array is: Array (1=> ' value ';value=> ' value ';0=> ' key ';key=> ' key ';)

Using each for array traversal

while ($ceil =each ($arr)) {

echo $ceil [0], ' ~ ', $ceil [1];

or echo $ceil [key], ' ~ ', $ceil [value];

}

Each is often used in conjunction with the list

List is a coincident assignment statement that can assign values to multiple variables at once

In practice, it is often used--assigning the cell allocation of an array to multiple variables

Example: $arr = Array (' Spring ', ' Summer ', ' Autumn ', ' Winter ');

List ($SPR, $sum, $fall, $win) = $arr;

Echo $SPR; Spring

Corresponds to the cell in the array from left to right in the list (starting with subscript 0)

If a cell in the array does not exist, it is treated as null

Example: List ($a, $b, $c) =array (2=> ' Zhao ',3=> ' money ',1=> ' Sun ');

echo $a, $b, $c; =>null Sun Zhao

Note: Assignments are left-to-right, and the process of assigning values is reversed

Example: List ($arr [0], $arr [1],, $arr [2]) =

Array (3=> ' Zhao ',1=> ' money ',5=> ' Sun ',2=> ' Lee ',0=> ' Yan ');

Print_r ($arr);

Assignment: Assignment:

$arr [0] = = array[0] $arr [2] = Zhao

$arr [1] = = Array[1] $arr [1] = money

+ array[2] $arr [0] = Yan

$arr [2] = array[3]

Array traversal using each and list mates

function For_each ($arr) {

while (list ($k, $v) each ($arr)) {

Echo $k, $v;

}

}

$arr = array (..);

For_each ($arr);

8. Array functions

Count (array variable, [true]); = = Count the number of cells in the array

The incoming can be an array, or it can be a non-array

The return value is divided according to the incoming parameters:

Array: Number of units (not entered in multidimensional arrays)

null:0

Non-array/non-null:1

When the optional parameter true is entered, the quantity is calculated as a recursive method

Recursion: That is, the number of calculations entered into multidimensional

such as: $arr = Array (' A ', ' B ', array (' C ', ' d '));

then echo count ($arr, true); =>5

In one dimension, there are: a B array () in two dimensions: C D A total of 5

Array_key_exists (' key ', $arr); = = Determines if the key is in the array

Isset ($arr [' key ']);

The difference:

Isset is a grammatical structure, eliminating the function call, fast

Single because it is based on the key value of the judgment, if there is a key value is NULL, then return FALSE

Summary: If the array is dynamically generated, it is not good to judge the key and value, with Array_key_exists insurance

Is_array (' value ', $arr); = = Determines if there is a value in the array

Array_flip ($arr); = = Swap the keys and values in the array

Range (start, end); = = Generates an array of a specified range

Array_sum ($arr); = = Calculates the value of all cells in the array and

Shuffle ($arr); = = disrupts the order of cell values in the array, returns a bool value, can be output using Print_r

Array_unique ($arr); = = Removes duplicate cell values from the array without changing the key name

Array_reverse ($arr, [true]); = = Reverse the cell values in the array

Sort ($arr); = = sort from small to large for cell values in arrays

The default does not affect the numeric index, which increases true to reverse the numeric index

About queues and stacks: Queues are FIFO first and stack is LIFO

Array_push ($arr, ' new unit '); = = Appends the cell to the tail of the array and returns the number of units

Array_pop ($arr); = = outputs the trailing cell of an array and removes it from the array

Array_unshift ($arr, ' new unit ') = adds a unit to the head of the array and returns the number of units

Array_shift ($arr); = = Outputs the array head cell and removes it from the array

If the index is numeric, it is re-counted by 0

@zhnoah
Source: http://www.cnblogs.com/zhnoah/
This article is owned by me and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original
The right to pursue legal liability.

PHP Learning Note--7. Array

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.