PHP section II data type array _php base

Source: Internet
Author: User
Tags constant

The array in PHP is actually an ordered map. A mapping is a type that associates values to the keys . This type is optimized in many ways, so you can think of it as a real array, or a list (vector), a hash list (an implementation of a map), a dictionary, a collection, a stack, a queue, and more possibilities. The value of an array element can also be another array. The tree structure and multidimensional array are also allowed.

You can use the array () language structure to create a new array. It accepts any number of comma-separated key (key) => values (value) pairs.

key 可以是 integer 或者 string。如果key是一个 integer 的标准表示,则被解释为整数(例如 "8" 将被解释为 8,而 "08" 将被解释为 "08")。key 中的浮点数被取整为 integer。在 PHP 中索引数组与关联数组是相同的,它们都可以同时包含整型和字符串的下标。

The value can be any PHP type.

If a key name is not 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 the key name to make integer 1 the key. Use FALSE as the key name to make integer 0 the key. Using null as a key name is equivalent to using an empty string. Creates a new (or overridden) value with an empty string as the key name using an empty string, unlike an empty square bracket.

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

New/modified with square brackets syntax

If$arr 还不存在,将会新建一个。这也是一种定义数组的替换方法。要改变一个值,只要给它赋一个新值。如果要删除一个键名/值对,要对它用 unset()。

Note: If you give a square bracket but do not specify a key name, the current maximum integer index value is taken, and the new key name is the value + 1. If you do not currently have an integer index, the key name is 0. If the specified key name already has a value, the value is overwritten.

Note that the largest integer key name used here is not necessarily in the array at the moment. It can only exist once the last array has been rebuilt index.

You should always enclose the array index in string notation with quotation marks. 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 the constant later, unfortunately, with the same name in your code. It works because PHP automatically converts a raw string (a string that has no quotes and does not correspond to any known symbol) to a normal string whose value is the bare string. For example, if there is no bar constant defined as, PHP will replace it with ' 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.