Page 1 of 1

PHP Beginner - display multi-dimensional arrays

Posted: Wed Jan 09, 2013 10:30 pm
by KerryH
Hi

I am a php newbie. I have written a small program to compare 2 csv files. I think I have got the 'comparison' part right, but I am stuck on how to display the different records from those 2 files in a nice table format?

file examples -

File 1 -
1000000001,CRUISE,TOM,BOB,MR,10-10-1999,2,2,4,8010,20,9225,41735383093112012,QSM2992 GOLD,200917574KA
1000000002,BIEBER,JUSTIN,ALEX,MR,11-11-1988,1,2,3,7010,20,9226,41535383093112012,QSM2892 GOLD,201017574KA

File 2 -

1000000001,CRUISE,TOM,BOB,MR,10-10-1999,2,2,4,8010,20,9225,41735383093112012,QSM2992 GOLD,200917574KA
1000000004,GATES,BILL,BOB,MR,10-10-1999,2,2,4,8010,20,9225,41735383093112012,QSM2992 GOLD,200917574KA

I want to display the records that are different (which is the second record in this case) like this -

File 1 File 2

ID 1000000001 1000000004
First Name Tom Bill
Last Name Cruise Gate

...etc...


What I have so far -

Populate the 2 multi-dimension arrays using code below

-

Code: Select all

if (($handle = fopen("file1.csv", "r")) !== FALSE) {
    # Set the parent multidimensional array key to 0.
    $nn1 = 0;
    while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
        # Count the total keys in the row.
        $c1 = count($data);
        # Populate the multidimensional array.
        for ($x=0;$x<$c1;$x++)
        {
            $csvarray1[$nn1][$x] = $data[$x];
			echo "$csvarray1($nn1)($x) = ". $csvarray1[$nn1][$x] . "<br />\n";
			echo "<br />\n";
        }
        $nn1++;
		echo "<br />\n";
    }
    # Close the File.
    fclose($handle);
}
Then I compare each field in each Array -

Code: Select all

$a = 0;
$b = 0;
# start comparing each field in array 1 and array 2
for ($a=0;$a<$nn1;$a++)
{
	for($b=0;$b<$c1;$b++) 
	{
	if ( ($csvarray1[$a][$b] != $csvarray2[$a][$b]) )
		{
			echo "<br /> \n";
			echo "File 1: ". "$csvarray1($a)($b) = ".$csvarray1[$a][$b]." is NOT equal to File 2: ". "$csvarray2($a)($b) = ".$csvarray2[$a][$b]."<br /> \n";			
			echo "<br /> \n";
			
		}
	else echo "File 1 " . "$csvarray1($a)($b) is equal to " . "File 2 $csvarray2($a)($b) <br /> \n";
	}
}

At the moment I am using 'echo' to display, can someone help me to put them in a nice table format?

Thanks in advance.

Re: PHP Beginner - display multi-dimensional arrays

Posted: Wed Jan 09, 2013 11:01 pm
by Christopher
KerryH wrote:At the moment I am using 'echo' to display, can someone help me to put them in a nice table format?
No sure what a nice table format is, but the basic way is:

Code: Select all

echo "<table>\n";
for ($a=0;$a<$nn1;$a++) {
    echo "<tr>\n<td>\n";
    echo "File 1: ". "$csvarray1($a)($b) = ".$csvarray1[$a][$b]." is NOT equal to File 2: ". "$csvarray2($a)($b) = ".$csvarray2[$a][$b]."\n";                       
    echo "</td>\n</tr>\n";
}
echo "</table>\n";
You can add "</td><td>" in the "File 1:" line to add more columns.

Re: PHP Beginner - display multi-dimensional arrays

Posted: Wed Jan 09, 2013 11:04 pm
by requinix
One "nice table format" would be, well, an actual table. To get the

Code: Select all

File 1 File 2

ID 1000000001 1000000004
First Name Tom Bill
Last Name Cruise Gate
output you showed the HTML could be like

Code: Select all

<table>
    <tr>
        <th></th>
        <th>File 1</th>
        <th>File 2</th>
    </tr>
<!-- repeat: -->
    <tr>
        <th>ID</th>
        <th>1000000001</th>
        <th>1000000004</th>
    </tr>
    <tr>
        <th>First Name</th>
        <td>Tom</td>
        <td>Bill</td>
    </tr>
    ...
    <tr>
        <th>Whatever that last field is</th>
        <td>201017574KA</td>
        <td>201017574KA</td>
    </tr>
<!-- /repeat, or give each row its own entire table -->
</table>
Breaking it down so each piece of data has its own HTML element means you can style the output. Like give a different background to cells with different data than the normal cells with matching data.

Re: PHP Beginner - display multi-dimensional arrays

Posted: Wed Jan 09, 2013 11:28 pm
by KerryH
Christopher wrote:
KerryH wrote:At the moment I am using 'echo' to display, can someone help me to put them in a nice table format?
No sure what a nice table format is, but the basic way is:

Code: Select all

echo "<table>\n";
for ($a=0;$a<$nn1;$a++) {
    echo "<tr>\n<td>\n";
    echo "File 1: ". "$csvarray1($a)($b) = ".$csvarray1[$a][$b]." is NOT equal to File 2: ". "$csvarray2($a)($b) = ".$csvarray2[$a][$b]."\n";                       
    echo "</td>\n</tr>\n";
}
echo "</table>\n";
You can add "</td><td>" in the "File 1:" line to add more columns.
Sorry, I don't seem to be able to get this to work. There are 2 'for' loops, which one should I put the <table> tag around?

Re: PHP Beginner - display multi-dimensional arrays

Posted: Thu Jan 10, 2013 5:11 pm
by Christopher
KerryH wrote:Sorry, I don't seem to be able to get this to work. There are 2 'for' loops, which one should I put the <table> tag around?
I think I would put the <tabl>e tags outside both loops. Then you could output a heading row in the outer loop and then output data rows in the inner loop.