Back to DFS's Workshop Page
Back to Agenda Page


Arrays III: Using Two-Dimensional Arrays

Quick Links

PHP possesses an interesting feature: Data of any type can be placed in any element of an array. This means that you can have numbers in one column of an array and text in another column.

This allows you to store all of the data about a given person or thing in a single row of an array. In this way it functions very much like a spreadsheet. For example, the table below contains both text data and integers. The batting average, which appears to be of type float, must actually be treated as a string so that a leading zero is not printed.

First Name Last Name AB H BA
Smoky Burgess 337 99 .294
Roberto Clemente 570 179 .314
Bill Mazeroski 539 147 .273

Initialization

One Field per Assignment Statement

You could use the following code which uses a separate assignment statement for each element in the array.

$playerinfo[0][0] = "Smoky";
$playerinfo[0][1] = "Burgess";
$playerinfo[0][2] = "337";
$playerinfo[0][3] = "99";
$playerinfo[0][4] = .294;
$playerinfo[1][0] = "Roberto";
$playerinfo[1][1] = "Clemente";
$playerinfo[1][2] = "570";
$playerinfo[1][3] = "179";
$playerinfo[1][4] = ".314";
$playerinfo[2][0] = "Bill";
$playerinfo[2][1] = "Mazeroski";
$playerinfo[2][2] = "539";
$playerinfo[2][3] = "147";
$playerinfo[2][4] = substr( sprintf( "%.3f", 147 / 539), 1);

One Record per Assignment Statement

Or the array can be initialized by using code similar to the following, which initializes one record at a time.

$playerinfo[] = array( "Smoky", "Burgess", 337, 99, .294 );
$playerinfo[] = array( "Roberto", "Clemente", 570, 179, ".314" );
$playerinfo[] = array( "Bill", "Mazeroski", 539, 147, substr( sprintf( "%.3f", 147 / 539), 1) );

Checking Values

If we execute print_r($playerinfo), we would get

Array
(
    [0] => Array
        (
            [0] => Smoky
            [1] => Burgess
            [2] => 337
            [3] => 99
            [4] => 0.294
        )

    [1] => Array
        (
            [0] => Roberto
            [1] => Clemente
            [2] => 570
            [3] => 179
            [4] => .314
        )

    [2] => Array
        (
            [0] => Bill
            [1] => Mazeroski
            [2] => 539
            [3] => 147
            [4] => .273

        )

)

The array could also be visualized as follows.

  0 1 2 3 4
0 Smoky Burgess 337 99 0.294
1 Roberto Clemente 570 179 .314
2 Bill Mazeroski 539 147 .273

Using Values

Printing Data in a Table

The code to print the information from the array could look like the following.

<table align=center cellpadding=3 border>
<tr>
<th>Name</th>
<th>AB</th>
<th>H</th>
<th>BA</th>
</tr>
<?php
foreach( $playerinfo as $pi )
{
   echo "<tr>\n";
   echo "<td>" . $pi[0] . " " . $pi[1] . "</td>\n";
   echo "<td align=right>" . $pi[2] . "</td>\n";
   echo "<td align=right>" . $pi[3] . "</td>\n";
   echo "<td align=right>" . substr( sprintf( "%.3f", $pi[3] / $pi[2] ), 1 ) . "</td>\n";
   echo "</tr>\n";
}
?>
</table>

The code above could be used to print the table below.

Name AB H BA
Smoky Burgess 337 99 .294
Roberto Clemente 570 179 .314
Bill Mazeroski 539 147 .273

Writing to a File

If you want to save information of this type for later use, a disk file is one choice. You can separate the fields with tab characters, \t, 0x09, and terminate each record with a newline, \n, 0x0A. The following code will do the job.

$fp = fopen("1960PiratesData.txt", "w");
for( $i = 0; $i < 3; $i++ )
{
   for( $j = 0; $j <= 3; $j++ )
   {
      fwrite( $fp, $playerinfo[$i][$j] . "\t" );
   }
   fwrite( $fp, $playerinfo[$i][4] . "\n" );
}
fclose($fp);

Reading from a File

If you want to read the information back from the disk file, the following code will do the job.

$fp = fopen("1960PiratesData.txt", "r");
while( $line = trim( fgets( $fp, 512 ) ) )
{
   $playerinfo[] = explode("\t", $line);
}
fclose($fp);

Note the square brackets on the $playerinfo name. explode() returns an array which is stored in the autoincremented array called $playerinfo.


© 2005 DFStermole
Created: 9 Dec 05
Last Modified: 9 Dec 05