Specifications

The code in Listing 3.3 loads the entire file into an array but unlike the example in Listing 3.2,
here we are using the function explode() to split up each line, so that we can apply some pro-
cessing and formatting before printing.
The output from this script is shown in Figure 3.6.
Using Arrays
C
HAPTER 3
3
USING ARRAYS
87
FIGURE 3.6
After splitting order records with explode, we can put each part of an order in a different table cell for better looking
output.
The explode function has the following prototype:
array explode(string separator, string string)
In the previous chapter, we used the tab character as a delimiter when storing this data, so here
we called
explode( “\t”, $orders[$i] )
This explodesthe passed-in string into parts. Each tab character becomes a break between
two elements. For example, the string
“15:42, 20th April\t4 tires\t1 oil\t
6 spark plugs\t$434.00\t22 Short St, Smalltown”
is exploded into the parts “15:42, 20th April”, “4 tires”, “1 oil”, “6 spark plugs”,
“$434.00”, and “22 Short St, Smalltown”.
We have not done very much processing here. Rather than output tires, oil, and spark plugs on
every line, we are only displaying the number of each and giving the table a heading row to
show what the numbers represent.
05 7842 CH03 3/6/01 3:42 PM Page 87