array
array array([mixed variable], [mixed ...])
|
variable
|
Variable to place into an array element
|
|
...
|
Further elements
|
Creates an array from provided values or key => value pairs.
Returns:
Array formed of the values or key/value pairs given as arguments
Description:
array() is a language construct used to create an array with zero or more elements. Each argument passed to the function is used as a single element for the array.
By default, the construct creates a numerically indexed array such as $array[0]. The indices start at 0 and increase by one for every element in the array. For example, this would create an array with five elements:
$list = array ('alpha', 'zappa', 'bravo', 4, 2);
The first element ('alpha') would be stored at key 0, and the second element ('zappa') would be stored at key 1.
String indexes and specific numeric indexes can be created using the special key => value syntax when calling array(). For example,
$jim = array ("birthday" => "1967/09/21", "favorite cake" => "Coconut");
would create a two-element array where the first value could be accessed via $jim['birthday'] and the second element could be accessed via $jim['favorite cake'].
Calls to array () can be nested to create multidimensional arrays.
array() doesn't return a special value on failure, as it will only fail on syntax errors that stop the script anyway.
Example:
Basic use of array()
// Create a three-element numerically indexed array
$my_array = array("one", "two", "three");
// Create a three-element string indexed array
$assoc_array = array("a"=>"x", "b"=>"y", "c"=>"z");
// Create a mixed string and numerically indexed array
$mixed_array = array("foo"=>"bar", "COBOL","Pascal");
// Create a multidimensional array
$multi_array = array(array(1, 2, 3), array("a", "b", "c"));
|