PHP » Arrays » array()

Syntax:
array array([mixed element1 [, mixed element2, ...]])
element1
The first element of the array.
element2
The second element of the array.

Creates an array from the parameters given.

This function takes a number of parameters to use as elements and returns an array with those elements. It can be used both for indexed and associative arrays.

Examples

Code:
<?php

$fruit = array("apple", "orange", "banana");
$fruit[3] = "pear";
$fruit[] = "peach";

$couples = array("Jonathan" => "Mindy",
   "Adam" => "Danielle",
   "Joe" => "Pamela");
$couples["Donald"] = "Daisy";

for ($i = 0; $i < sizeof($fruit); $i++) {
   print "$fruit[$i] ";
}

print "<br><br>";

$wife = current($couples);
do {
   print key($couples) . " & $wife <br>";
} while ($wife = next($couples))

?>
Output:
apple orange banana pear peach

Jonathan & Mindy
Adam & Danielle
Joe & Pamela
Donald & Daisy
Explanation:

This example shows how both associative and index arrays can be created with array(). It also demonstrates a couple of methods for adding elements.

See Also: