PHP section 2 data type array

Source: Internet
Author: User

Arrays in PHP are actually an ordered ing. Ing isValuesAssociatedKeys. This type has been optimized in many aspects, so it can be considered as a real array, or a list (vector), a hash list (an implementation of ing), a dictionary, a set, stack, queue, and more possibilities. The value of an array element can also be another array. Tree Structure and multi-dimensional array are also allowed.

You can use the array () language structure to create an array. It accepts any number of comma-separatedKey => value)Yes.

KeyIt can be integer or string. If the key is a standard integer, it is interpreted as an integer (for example"8"Will be interpreted8, And"08"Will be interpreted"08").KeyThe floating point number in is rounded to integer. In PHP, the index array and the associated array are the same. They can both contain the subscript of the integer and string.

The value can be of any PHP type.

If no key name is specified for the given value, the current maximum integer index value is used, and the new key name is the value plus one. If the specified key name already has a value, the value will be overwritten.

UseTRUEUse integer as the key name1Become the key name. UseFALSEUse integer as the key name0Become the key name. UseNULLAs a key name, it is equivalent to using a null string. Use an empty string as the key name to create (or overwrite) a new value with an empty string as the key name, which is different from the empty square brackets.

Arrays and objects cannot be used as keys ). This will cause a warning:Illegal offset type.

Create/modify with square brackets

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

Note: If square brackets are provided but no key name is specified, the current maximum integer index value is taken. The new key name is the value + 1. If no integer index exists, the key name is0. If the specified key name already has a value, the value will be overwritten.

Note that the maximum integer key used hereNot necessarilyIt is in the array. It only needs to exist after the index is re-generated in the last array.

Always put quotation marks on the array index represented by a string. For example, use $ foo ['bar'] instead of $ foo [bar]. But why is $ foo [bar] Wrong?

The reason is that this code has an undefined constant (bar) instead of a string ('bar'-note quotation marks), and PHP may define this constant later, unfortunately, your code has the same name. It can run because PHP automaticallyBare string(A string without quotation marks and should not contain any known symbols) is converted into a normal string whose value is the bare string. For example, if no constant is definedbar, PHP will replace it'Bar'And use it.

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.