Home > Articles > Programming > PHP

  • Print
  • + Share This

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 current order file.

Listing 3.2 vieworders.php—Using 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.

Listing 3.3 vieworders2.php—Using PHP to Separate, Format, and Display Orders for Bob

 <title>Bob's Auto Parts – Customer Orders</title>
<h1>Bob's Auto Parts</h1>
<h2>Customer Orders</h2>
  //Read in the entire file.
  //Each order becomes an element in the array
  $orders= file("../../orders/orders.txt");
  // count the number of orders in the array
  $number_of_orders = count($orders);
  if ($number_of_orders == 0)
   echo "<p><strong>No orders pending.
     Please try again later.</strong></p>";
  echo "<table border=1>\n";
  echo "<tr><th bgcolor = \"#CCCCFF\">Order Date</td>
       <th bgcolor = \"#CCCCFF\">Tires</td>
       <th bgcolor = \"#CCCCFF\">Oil</td>
       <th bgcolor = \"#CCCCFF\">Spark Plugs</td>
       <th bgcolor = \"#CCCCFF\">Total</td>
       <th bgcolor = \"#CCCCFF\">Address</td>
  for ($i=0; $i<$number_of_orders; $i++)
   //split up each line
   $line = explode( "\t", $orders[$i] );
   // keep only the number of items ordered
   $line[1] = intval( $line[1] );
   $line[2] = intval( $line[2] );
   $line[3] = intval( $line[3] );
   // output each order
   echo "<tr><td>$line[0]</td>
        <td align = right>$line[1]</td>
        <td align = right>$line[2]</td>
        <td align = right>$line[3]</td>
        <td align = right>$line[4]</td>
  echo "</table>";

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 processing and formatting before printing.

The output from this script is shown in Figure 3.6.

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 "explodes" the 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.

There are a number of ways that we could have extracted numbers from these strings. Here we used the function, intval(). As mentioned in Chapter 1, intval() converts a string to an integer. The conversion is reasonably clever and will ignore parts, such as the label in this example, that cannot be converted to an integer. We will cover various ways of processing strings in the next chapter.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.