Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

Combine Overly-Redundant Table Of Data (PHP/MySQL)

$
0
0
I am fetching an HTML table using the Simple HTML DOM Parser. I get $col[0], $col[1], $col[3] and $col[4] for each row in the HTML table, these are columns. This is ran through a foreach to loop through every row. For testing purposes, I output the following:
 
$col[0] --- $col[1] --- $col[2] --- $col[3]
 
11:23:26 PM ---- MEDICAL ---- 41ST ST ---- E19
11:23:26 PM ---- MEDICAL ---- 41ST ST ---- M12
10:45:26 PM ---- MEDICAL ---- MAIN ST ---- E20
10:45:26 PM ---- MEDICAL ---- MAIN ST ---- M62
10:45:26 PM ---- MEDICAL ---- MAIN ST ---- T20
09:49:19 PM ---- RESCUE ---- BROADWAY AVE ---- E27
 
The first three colums are always identical for the grouping. The third column is what is different. I want to combine the data like so:
 
11:23:26 PM ---- MEDICAL ---- 41ST ST ---- E19, M12
10:45:26 PM ---- MEDICAL ---- MAIN ST ---- E20, M62, T20
09:49:19 PM ---- RESCUE ---- BROADWAY AVE ---- E27
 
How would I detect that "if columns 1, 2 and 3 are the same, then take the data from column 4 for each row"? Note that the number of rows could fluctuate between 1 to around 10-15 on average. Each row specifies a different apparatus/unit (this is a fire department listing of incidents), but this wastes a lot of vertical space and is overly redundant.
 
My specific code is below:
 

<?php

require 'simple_html_dom.php';

$url = "http://www.website.com/html_table.html";

$sHtml = file_get_contents($url);

$oHTML = str_get_html($sHtml);
$oTRs = $oHTML->find('table tr');
$aData = array();

foreach($oTRs as $oTR) {
	$aRow = array();
	$oTDs = $oTR->find('td');

	foreach($oTDs as $oTD) {
		$aRow[] = trim($oTD->plaintext);
	}

	echo $aRow[0];
	echo " ---- ";
	echo $aRow[1];
	echo " ---- ";
	echo $aRow[2];
	echo " ---- ";
	echo $aRow[3];
	echo "<br>\n";

	$aData[] = $aRow;
}

?>

Viewing all articles
Browse latest Browse all 13200

Trending Articles