Creating Arrays

Numerical Arrays

In PHP, the indices start at zero by default.

To assign values to an array, use array():

$fruits = array('Apples', 'Grapes', 'Bananas', 'Oranges');
The same array can be created as follows by using []:
$fruits[0] = "Apples";
$fruits[1] = "Grapes";
$fruits[2] = "Bananas";
$fruits[3] = "Oranges";
and:
$fruits[] = "Apples";
$fruits[] = "Grapes";
$fruits[] = "Bananas";
$fruits[] = "Oranges";

Assigning a value to an array with an empty subscript is shorthand for adding a new element to the end of the array. So PHP looks up the length of $fruits and uses that as the position for the value you're assigning.

To access the contents of an array, you use the variable name and a key or index. The key or index indicates which of the values in the array you access. The index is placed in spaqure brackets after the name.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN">
<head>
<title>Creating Arrays</title>
<meta Name="Author" Content="Hann So">
</head>
<body>
<p>
<?php
echo "<p><b>Use array() to create the array fruits.</b></p>";
$fruits = array('Apples', 'Grapes', 'Bananas', 'Oranges');
echo "$fruits[0]<br />";
echo "$fruits[1]<br />";
echo "$fruits[2]<br />";
echo "$fruits[3]<br />";
// Use []
$fruits2[0] = "Apples";
$fruits2[1] = "Grapes";
$fruits2[2] = "Bananas";
$fruits2[3] = "Oranges";
echo "<p><b>Use [] to create the array fruits2.</b></p>";
for ($i = 0; $i<4; $i++)
	echo "$fruits2[$i]<br />";
// Use []
$fruits3[] = "Pears";
$fruits3[] = "Watermelons";
$fruits3[] = "Cantaloupes";
$fruits3[] = "Dates";
echo "<p><b>Use [] to add to the array fruits3.</b></p>";
// Use the count function to find the number of elements in an array
for ($i = 0; $i<count($fruits3); $i++)
	echo "$fruits3[$i]<br />";
?>
</p>
</body>
</html>

View the effect

Associative Arrays

To define an array using string keys, you can also use , but specify the key/value pairs with =>:

$fruits = array('red'=>'Apples', 'green'=>'Grapes', 'yellow'=>'Bananas', 'orange'=>'Oranges');
This is shorthand for:
$fruits['red'] = "Apples";
$fruits['green'] = "Grapes";
$fruits['yellow'] = "Bananas";
$fruits['orange'] = "Oranges";

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN">
<head>
<title>PHP</title>
<meta Name="Author" Content="Hann So">
</head>
<body>
<p>
<?php
$fruits = array('red'=>'Apples', 'green'=>'Grapes', 'yellow'=>'Bananas', 'orange'=>'Oranges');
// use foreach to loop through the array
echo "<p><b>Use array() to create the associative array fruits.</b></p>";
foreach ($fruits as $color => $value) {
	echo "$value are $color<br />";
}
// Use []
$fruits2[red] = "Apples";
$fruits2[green] = "Grapes";
$fruits2[yellow] = "Bananas";
$fruits2[orange] = "Oranges";
echo "<p><b>Use [] to create the associative array fruits.</b></p>";
foreach ($fruits2 as $color => $value) {
	echo "$value are $color<br />";
}
?>
</p>
</body>
</html>

View the effect


Arrays | What is an array? | Creating Arrays | Iterating | Not Beginning at 0 | Initializing to a Range | Modifying | Removing Elements | Removing Duplicate | Changing Size | Key in an Array | Element in an Array | Position of a Value | Largest or Smallest Element | Reversing | Randomizing
© 2008: Hann So
email: hso@voyager.deanza.edu