Section 2 an array of data types

  • 2020-05-16 06:31:41
  • OfStack

The array in PHP is actually an ordered map. The mapping is a type that associates values to keys. This type is optimized in many ways, so think of it as a real array, or list (vector), hash table (an implementation of mapping), dictionary, collection, stack, queue, and more. 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 keys (key) = > Value (value) right.

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

The value can be of 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 1. 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 name. integer 0 becomes the key name using FALSE as the key name. Using NULL as the key name is equivalent to using an empty string. Using an empty string as the keyname creates (or overrides) a value with an empty string as the key name, as opposed to using empty square brackets.

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

Create/modify using the square brackets syntax

If $arr does not already exist, a new one will be created. This is also one way to define an array substitution. To change a value, just assign it a new value. If you want to delete a key name/value pair, use unset() for 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 is + 1. If there is no current integer index, the key name will be 0. If the specified key name already has a value, the value is overwritten.

Note that the maximum integer key name used here is currently in the array. It only needs to have existed since the last time the array was regenerated.

You should always put quotes around the index of an array represented as 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 the quotation marks), and PHP may define this constant later, unfortunately you have the same name in your code. It works because PHP automatically converts a naked string (a string without quotes and not corresponding to any known symbol) to a normal string whose value is the naked string. For example, if no constant is defined as bar, PHP will replace it with 'bar' and use it.


Related articles: