Just for the record (in case someone chances upon this thread in the future), I decided to go with this code:
// The dilemma: get values from 2 different tables, then display a report with
// ALL the data, but also merge the two tables where their indexes match.
// OPTIONALLY display detailed data (not coded here)
// Outer values...
mysql_query("SELECT location FROM Locations", $link);
while($row = mysql_fetch_array)
{
$location = $row['location'];
// Populate array 1...
mysql_query("SELECT index1, value1 FROM table1 WHERE location = $location", $link);
while($row = mysql_fetch_array($result))
{
$key = $row['index1'];
$value = $row['value1'];
$array1[$location][$key] = $value;
}
// Populate array 2...
mysql_query("SELECT index1, value2 FROM table2 WHERE location = $location", $link);
while($row = mysql_fetch_array($result))
{
$key = $row['index1'];
$value = $row['value2'];
$array2[$location][$key] = $value;
}
// Here's the solution part...
$keys1 = array_keys($array1[$location]);
$keys2 = array_keys($array2[$location]);
$keys_not_in_array1 = array_diff($keys2, $keys1);
}
// NOTE: I'm leaving out the 3rd "detail" element of the report here,
// which is the main reason for using arrays...
// Display the report...
foreach($array1 as $key => $value)
{
echo "<table><tr><td colspan='3'><strong>Location: $key</strong></td></tr>";
// Iterate through $value (which is actually $array1)...
foreach($value as $key => $value)
{
echo "<tr><td>$key</td><td>$value</td>";
// Display matching indexes from array2, if any...
if(isset($array2[$key]))
echo "<td>$array2[$key]</td><tr>";
else
echo "<td></td></tr>";
}
// Iterate through the remaining un-matched values...
foreach($keys_not_in_array1 as $key => $value)
{
echo "<tr><td>$key</td><td></td><td>$value</td></tr>";
}
}