Definition
The array() function is used to create a PHP array.
Syntax
- Indexed arrays
array(value1, value2, value3, ...)
- Associative arrays
array(key=>value, key=>value, key=>value, ...)
Parameters
Parameter | Description |
---|---|
key |
Numeric or String. Specifies the key. |
value |
Specifies the value on the given key. |
Example
<?php
// Indexed Array
$cities = array("New York", "Salt Lake", "Tokyo");
var_dump($cities);
// Associative Array
$ages = array("Mark" => 22, "Jeff" => 32, "Mike" => 28);
foreach($ages as $x => $x_value) {
echo "Name = " . $x . ", Age = " . $x_value;
echo "<br>";
}