Specifications

Alternatively, we can use the array_reverse() function to reverse the array created by
range().
$numbers = range(1,10);
$numbers = array_reverse($numbers);
Note that array_reverse() returns a modified copy of the array. Because we did not want the
original array, we simply stored the new copy over the original.
Loading Arrays from Files
In Chapter 2, Storing and Retrieving Data, we stored customer orders in a file. Each line in
the file looks something like
15:42, 20th April 4 tires 1 oil 6 spark plugs $434.00 22 Short St, Smalltown
To process or fulfill this order, we could load it back into an array. Listing 3.2 displays the cur-
rent order file.
LISTING 3.2 vieworders.phpUsing PHP to Display Orders for Bob
$orders= file(“../../orders/orders.txt”);
$number_of_orders = count($orders);
if ($number_of_orders == 0)
{
echo “<p><strong>No orders pending.
Please try again later.</strong></p>”;
}
for ($i=0; $i<$number_of_orders; $i++)
{
echo $orders[$i].”<br>”;
}
This script produces almost exactly the same output as Listing 2.2 in the previous chapter,
which is shown in Figure 2.4. This time, we are using the function file(), which loads the
entire file into an array. Each line in the file becomes one element of an array.
This code also uses the count() function to see how many elements are in an array.
Furthermore, we could load each section of the order lines into separate array elements to
process the sections separately or to format them more attractively. Listing 3.3 does exactly
that.
Using Arrays
C
HAPTER 3
3
USING ARRAYS
85
05 7842 CH03 3/6/01 3:42 PM Page 85