Modifying Arrays

Modifying an array is easy. You assign a new value to the nelement of the array that you changed.

<!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
echo "<p><b>Use array() to create the array fruits.</b></p>";
$fruits = array('Apples', 'Grapes', 'Bananas', 'Oranges');
foreach ($fruits as $value)
	echo "$value ";
// Change the value for 3r element
$fruits[2] = "Pears";
// Add a new element
$fruits[] = "Dates";
echo "<p><b>Modify and add.</b></p>";
foreach ($fruits as $value)
	echo "$value ";
// Copy the whole array
$fruits2 = $fruits;
echo "<p><b>Copy the whole array.</b></p>";
foreach ($fruits2 as $value)
	echo "$value ";
?>
</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