Overview
An array in PHP is actually an ordered map, which is a type that associates values with the keys.
PHP arrays can contain both integer and string key names, because PHP does not actually differentiate between indexed and associative arrays.
Key (key) can be an integer or string
Values (value) can be any type of value
Definition of an array
There are two ways of
You can use the array () language structure to create a new array
array( key => value , ... )
Since 5.4 You can use the short array definition syntax, substituting [] for array ()
The comma after the last array element can be omitted. Typically used in a single-line array definition, such as a common array (1, 2) instead of an array (1, 2,). The last comma is usually reserved for multi-line array definitions, which makes it easier to add a new cell.
The keys of the array
The key (key) is an integer or string
In addition, key will have the following casts
- A string containing a valid integer value is converted to an integral type. For example, the key name "8" will actually be stored as 8. However, "08" does not cast because it is not a valid decimal value.
- Floating-point numbers are also converted to integers, meaning that their fractional parts are removed. For example, key name 8.7 will actually be stored as 8.
- Boolean values are also converted to integral types. That is, the key name true is actually stored as 1 and the key name false is stored as 0.
- Null is converted to an empty string, that is, the key name null is actually stored as "".
- Arrays and objects cannot be used as key names. Insisting on doing so will result in a warning: illegal offset type.
If more than one cell in the array definition uses the same key name, only the last one is used, and the previous is overwritten.
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, and if no integer index is currently present, the key name is 0.
$foo[' bar '] with $Foo[bar]
For $foo[bar], if there is no constant defined as bar,php will replace it with ' bar ' and use it
Traversal of an array
The foreach syntax structure provides an easy way to iterate through an array. foreach can only be applied to arrays and objects.
There are two kinds of syntax:
foreach (array_expression as $value) statementforeach (array_expression as $key => $value) statement
Unset ()
The unset () function allows you to delete a key in an array. However, be aware that the array will not rebuild the index. If you need to rebuild the index after deletion, you can use the Array_values () function.
$a = array(1 => 'one', 2 => 'two', 3 => 'three');unset($a[2]);/* will produce an array that would have been defined as $a = array(1 => 'one', 3 => 'three'); and NOT $a = array(1 => 'one', 2 =>'three');*/$b = array_values($a);// Now $b is array(0 => 'one', 1 =>'three')
Array functions
http://php.net/manual/zh/ref.array.php
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The above describes the PHP array, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.