http://www.chrispearson.org/pages/programming/php/array04.asp
20h28
Monday, 1. December 2008

PHP: ARRAYS

PROCESSING ARRAYS

This section will present several examples which will demonstrate ways to process arrays in PHP - the first uses an array to pass form data.

PASSING FORM DATA

To begin with, create a PHP file with the basic HTML framework and save it as selectgoods.php. Save a second copy of the file as shopper.php. We'll use these as in the exercise.

Open selectgoods.php and create a form on the page, as shown on the right (Only code for the document body is shown here)

As you can see this is a fairly straightforward HTML form except that, instead of giving each of the form's text boxes a name like bakery or drinks, we have assigned an element of an associative array as the textbox object's name.

When the form is submitted (clicking on the form's submit button) the form data will be posted to the named page: shopper.php

Save the file to selectgoods.php and browse to it to check it out (At this stage, if you click on Submit the shopper.php page will load but it should be blank)

<body>
< form name="frmGoods"
   id="frmGoods" method="post"
   action="shopper.php">
<p>Please select your shopping by naming goods in each category</p>
<p>Bakery
<input name="Array[bakery]" type="text" id="bakery" />
</p>
<p>Drinks
<input name="Array[drinks]" type="text" id="drinks" />
</p>
<p>Fruit
<input name="Array[fruit]" type="text" id="Fruit" />
</p>
<p>Vegetables
<input name="Array[veggies]" type="text" id="Veggies" />
</p>
<p>
<input type="submit" name="Submit" value="Submit" />
</p>
</form>
</body>

Next, open shopper.php for editing and add the code shown to the right. This page shows a brief introductory line of text, outputs the posted information from selectgoods.php, then signs off with a jolly marketing message

With the text and code in place between the <body> tags, save shopper.php

<body>
<p>We have received your shopping list and will meet your requirements as follows</p>
<p>
<?PHP
while (list($index, $item) = each($Array)) {
print("$index - $item<BR>\n");
}
?>
</p>
<p> Thank you for shopping with NonExistent Web Shops </p>
</body> 

Browse to selectgoods.php and enter your shopping requirements, as requested

Then click on Submit: shopper.php should now load, accepting the passed information from the form

Through HTTP shopper.php has accepted Array from selectgoods.php and, in the while() loop has sent the category name (the element's key) and the product type (the element's value) to the browser.

In the next example we'll see how an element can be added to the array.

In this scenario the online shop adds a free gift to every shopper's list of requirements

Open shopper.php for editing and amend the PHP code, as shown

Notice that the key to an associative array can contain any characters, even spaces - Once the key is named and the element assigned a value it is created within the array, producing the output to the browser as shown below, right

Save the updated shopper.php file and browse to selectgoods.php and try it out

<body>
< p>We have received your shopping list and will meet your requirements as follows</p>
< p>
< ?PHP

$Array["free gift"] = "PHP wallchart";
while (list($index, $item) = each($Array)) {
print("$index - $item<BR>\n");
}
?>
< /p>
< p> Thank you for shopping with NonExistent Web Shops </p>
< /body>
  Output after adding the free gift element

Next we'll use unset() to remove an element from an array.

In this scenario, we know the online store will never supply alcoholic beverages so we check that beer isn't ordered in the drinks category.

Open shopper.php for editing again and make the changes shown - Notice that we loop through the array removing any offending requirements the use reset() to place the array pointer back to the first element. If we didn't do this we'd enter the second while() loop still pointing at the end of the array and the second loop would create no output.

<?PHP
while (list($index, $item) = each($Array)) {
if ($item == "Beer") {
unset($Array[$index]);
print("Please note that we cannot supply alcoholic beverages<BR>Your amended list is<BR>");
}
}
reset($Array);
$Array["free gift"] = "PHP wallchart";
while (list($index, $item) = each($Array)) {
print("$index - $item<BR>\n");
}

?>
Some other useful array functions are shown here
each Moves the pointer to the next array element (we've already used this one)
current() Returns the element currently pointed to
reset() Returns the pointer to the first element
end() Sets the pointer to the last element
next() Moves the pointer on by one element
prev() Moves the pointer back one place
array_walk() Applies the same action the every member of an array
With the changes in place save shopper.php and browse to selectgoods.php - Enter the same old list and you will get the same old response.
 
But change your drinks requirement to Beer and see what the result is.

One point to note is that unset() does, quite literally, remove the specified element from an array. It does nothing to close up the hole a deletion might make in the index sequence.

Notice, then, the output produced when unset() is used on a numerically indexed array - The first loop outputs the contents of the array, the second loop outputs the amended array: The results are quite logical because PHP treats all array keys in the same way, whether they are integer numbers of complicated strings. Bear in mind, too, that when using a counted loop (like a for() loop) removing an element will change the element count.

This is demonstrated in the page unsetdemo.php

This will always output the whole array without errors because it steps sequentially through the array

It can be used on associative arrays as well as numerically index arrays

while (list($index, $item) = each($Array)) {
  print("$index - $item<BR>\n");
}

This for() loop will encounter an error is an element is removed from a numerically indexed array.

It can't be used on associative arrays

$elementcount = count($Array);
for ($i = 0; $i < $elementcount; $i++) {
  print("$i - $Array[$i]<BR>\n");
}
 

 

< Previous page: Arrays - Getting at their elements Next page: PHP reference sources>

xxx,xxx

copyright ©2000 - 2008 Chris Pearson