JavaScript data Type an array of data types in PHP section II

Source: Internet
Author: User

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. The value of an array element can also be another array. Tree structures and multidimensional arrays are also allowed.

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.

keyCan be an integer or a string. If key is a standard representation of an integer, it is interpreted as an integer (for example, "8" will be interpreted as 8, and " " " will be interpreted as" "). keyThe floating-point number in is rounded to integer. Indexed arrays are the same as associative arrays in PHP, and can contain both integer and string subscripts.

The value can be any PHP type.

If no key name is specified for the given value, the current largest integer index value is taken, and the new key name is the value plus one. If the specified key name already has a value, the value is overwritten.

Use TRUE as a key name to make the integer1 a key. Use FALSE as a key name to make the integer0 a key. Using null as the key name is equivalent to using an empty string. Create a new (or overwrite) value with an empty string as the key name using an empty string, which is not the same as the empty square brackets.

You cannot use arrays and objects as keys (key). Doing so will result in a warning:illegal offset type.

New/modified with square brackets syntax

If $arr it does not already exist, a new one will be created. This is also a substitution method for defining arrays. To change a value, simply assign it a new value. If you want to delete a key name/value pair, use unset ()for it.

Note: If the square brackets are given but no key name is specified, the current maximum integer index value is taken, and the new key name is the value + 1. If no integer index is currently present, the key name is 0. If the specified key name already has a value, the value will be overwritten.

Note that the maximum integer key name used here is not necessarily present in the array. It just has to exist after the last array rebuild index.

You should always enclose the array index with a string representation. For example, use $foo [' Bar '] instead of $foo [bar]. But why $foo [bar] wrong?

The reason is that there is an undefined constant (bar) in this code instead of a string (' bar '-note quotes), and PHP may define this constant later, but unfortunately your code has the same name. It works because PHP automatically converts a bare string (a string without a quotation mark) to a normal string whose value is the bare string, which does not correspond to any known symbol. For example, if no constant is defined bar , PHP will replace it with a ' bar ' and use it.

The above describes the JavaScript data type PHP second section of the data type array, including the JavaScript data type of content, I hope to be interested in PHP tutorial friends helpful.

  • 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.