Arrays
suggest changeIntroduction
An array is a data structure that stores an arbitrary number of values in a single value. An array in PHP is an ordered map: it associates values with keys.
Syntax
$array = array('Value1', 'Value2', 'Value3');— keys default to 0, 1, 2, …$array = array('Value1', 'Value2', );— optional trailing comma$array = array('key1' => 'Value1', 'key2' => 'Value2', );— explicit keys$array = array('key1' => 'Value1', 'Value2', );—array('key1' => 'Value1', 1 => 'Value2')$array = ['key1' => 'Value1', 'key2' => 'Value2', ];— short array syntax (PHP 5.4+)$array[] = 'ValueX';— append'ValueX'$array['keyX'] = 'ValueX';— assign'ValueX'to key'keyX'$array += ['keyX' => 'valueX', 'keyY' => 'valueY'];— array union: keys already present on the left are kept, not overwritten
Parameters
| Parameter | Detail |
|---|---|
| Key | The key identifies an element. It is an int or a string. Valid keys include 'foo', 10, and 'a2b'. A numeric string such as '5' is cast to the integer 5. |
| Value | Each key has a corresponding value. The value may be any type. Reading a key that is not set returns null and emits a warning (a notice before PHP 8). |
Remarks
See also
- Manipulating a single array
- Executing upon an array
- Array iteration
- Processing multiple arrays together
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents