Functions | arrays
1. New array using array
Array array ([mixed ...])
Returns an array created from a parameter. Parameters can be indexed using the => operator. For information about what the array is, read the section of the array.
Note: Array () is a language structure that literally represents an array, not a regular function.
Syntax "Index => values", separated by commas, defines indexes and values. An index can be a string or a number. If an index is omitted, an integer index starting at 0 is automatically generated. If the index is an integer, the next resulting index will be the current largest integer index + 1. Note If two identical indexes are defined, the latter one overwrites the previous one.
Adding a comma after the last defined array item is not uncommon, but is a legitimate syntax.
The following example shows how to create a two-dimensional array, assign a key name to the corresponding array, and skip and continue the numeric index in the normal array. Example 1. Array () example
<?php
$fruits = Array (
"Fruits" => Array ("A" => "Orange", "B" => "banana", "C" => "Apple"),
"Numbers" => Array (1, 2, 3, 4, 5, 6),
"Holes" => Array ("A", 5 => "second", "third")
);
?>
Example 2. Automatic indexing of Array ()
<?php
$array = Array (1, 1, 1, 1, 1, 8 => 1, 4 => 1, 3 => 13);
Print_r ($array);
?>
The example above will output:
Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 13
[4] => 1
[8] => 1
[9] => 19
)
Note that index 3 is defined two times and retains the last value of 13. Index 4 is defined after index 8, and the next auto-generated index (with a value of 19) is 9, because the largest index is 8.