Php-manual's Learning----"Language Reference"----"type"-----"array Array"

Source: Internet
Author: User
Tags array definition

1.Array arrays
The array in PHP is actually an ordered map. A map is a type that associates values to the keys. This type is optimized in many ways, so it can be used as a real array, or as a list (vector), a hash (an implementation of a mapping), a dictionary, a collection, a stack, a queue, and more possibilities. Because the value of an array element can also be another array, the tree structure and the multidimensional array are also allowed.
Explaining these structures is beyond the scope of this manual, but at least one example is provided for each structure. For more information on these structures, it is recommended to refer to other works on this broad topic.
2. Syntax:
Defines an array of arrays ()
You can use the array () language structure to create a new array. It accepts any number of key (key) = = VALUES (value) pairs separated by commas.
Array (key = value
, ...
)
The key (key) is an integer or string
Values (value) can be any type of value
The comma after the last array element can be omitted. Typically used in a single-line array definition, such as a common array (1, 2) instead of an array (1, 2,). The last comma is usually reserved for multi-line array definitions, which makes it easier to add a new cell.
You can use the short array definition syntax since 5.4 to replace array () with [].
Key can be an integer or string. Value can be of any type.
3. In addition, key will have the following casts:
1. A string containing a valid integer value is converted to an integral type. For example, the key name "8" will actually be stored as 8. However, "08" does not cast because it is not a valid decimal value.
2. Floating-point numbers are also converted to integers, meaning that their fractional parts are removed. For example, key name 8.7 will actually be stored as 8.
3. The Boolean value is also converted to an integer type. That is, the key name true is actually stored as 1 and the key name false is stored as 0.
4. Null is converted to an empty string, that is, the key name null is actually stored as "".
5. Arrays and objects cannot be used as key names. Insisting on doing so will result in a warning: illegal offset type.
If more than one cell in the array definition uses the same key name, only the last one is used, and the previous is overwritten.
4.PHP arrays can contain both integer and string key names, because PHP does not actually differentiate between indexed and associative arrays.
5.key is optional. If not specified, PHP will automatically use the maximum integer key name previously used plus 1 as the new key name.
6. Arrays can be set simultaneously.
7. New/modified with square brackets syntax
You can modify an existing array by explicitly setting its value.
This is done by assigning a value to the array by specifying the key name within the square brackets. You can also omit the key name, in which case a pair of empty brackets ([]) are added to the variable name.
$arr [key] = value;
$arr [] = value;
Key can be an integer or string
Value can be of any type
If the $arr does not already exist, a new one will be created, which is another way to create a new array. This is not encouraged, however, because if the $arr already contains a value (such as a string from a request variable) then this value is preserved and [] actually represents the string access operator. The best way to initialize a variable is to assign it directly.
To modify a value, assign a new value to the cell by its key name. To delete a key-value pair, call the unset () function on it.
The 8.foreach control structure is specifically used for arrays. It provides a simple way to iterate through an array.
9. Why is the $foo [bar] wrong?
You should always enclose the array index with a string representation. For example, use $foo [' Bar '] instead of $foo [bar]. But why? You may have seen the following syntax in the old script:
10.

Php-manual's Learning----"Language Reference"----"type"-----"array 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.