Home > Backend Development > PHP Tutorial > PHP Section 2 Array of Data Types_PHP Tutorial

PHP Section 2 Array of Data Types_PHP Tutorial

WBOY
Release: 2016-07-21 15:19:31
Original
906 people have browsed it

The array in PHP is actually an ordered map. A mapping is a type that associates values to keys. This type is optimized in many ways, so it can be treated as a real array, or a list (vector), a hash table (an implementation of a map), a dictionary, a set, a stack, a queue, and many 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 comma-separated key => value pairs.

key can be integer or string. If key is a standard representation of integer, it is interpreted as an integer (e.g. "8" will be interpreted as 8, while "08" will be interpreted as "08"). The floating point number in key is rounded to integer. In PHP, indexed arrays are the same as associated arrays, they can both contain integers and The subscript of the string .

The value can be any PHP type.

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

Using TRUE as a key name will make integer 1 the key name. Using FALSE as the key name will make integer 0 the key name. Using NULL as a key name will be equivalent to using the empty string. Using an empty string as a key will create (or overwrite) a value with an empty string as the key, which is different from using empty square brackets.

Arrays and objects cannot be used as keys. Doing so will result in a warning: Illegal offset type.

Create/modify using square bracket syntax

If $arr does not exist yet, a new one will be created. This is also an alternative way to define an array. To change a value, just assign it a new value. If you want to delete a key/value pair, use unset() on it.

Note: If square brackets are given but no key name is specified, the current maximum integer index value is taken, and the new key name will be that value + 1. If there is no integer index currently, the key name will be 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 currently in the array. It just needs to have existed since the last time the array was reindexed.

Array indexes expressed as strings should always be quoted. 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 the quotes), and PHP may define this constant later, unfortunately your code has the same name. It works because PHP automatically converts naked string (a string without quotes and not corresponding to any known symbol) into a normal string whose value is that bare string. For example, if there is no constant defined as bar, PHP will replace it with 'bar' and use that.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325301.htmlTechArticleAn array in PHP is actually an ordered map. A map is a type that associates values ​​to keys. This type has been optimized in many ways, so it can be treated as a real array...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template