组合数组不会显示在单个表格行中

时间:2013-12-14 20:59:20

标签: php arrays

我从数据库中的两个不同表中提取数据。一旦我从两者中获得了所有需求,我希望将每个数组和打印数据组合成一个HTML表格。

它们都结合在一起,但从它看起来array_merge将第二个数组附加到第一个数组,就像它应该的那样。但是,我的问题是,当我从新组合数组中的数据创建表时,它将在一行中显示我想要的第一个数组中的所有字段,在第二行上显示第二个数组中的所有数据。有没有办法强制array_merge 100%将数组合并为一个?

$names = Array (
 [0] => Array (
  [PointID] => -3621
  [AddressID] => -503718
  [CommonName] => AARONS
  [MapDataID] => -2 
  [AlternateAddressID] => 
  [GISObjectID] => 28 
  [DateChanged] => 2013-06-19 13:48:44.947 
  [EnteredDate] => 2010-09-21 08:21:26.847
 )
)

$addressInfo= Array (
 [0] => Array (
  [AddressID] => -503718
  [LocationType] => 1
  [HouseNumber] => XXXX
  [AdditionalInfo] =>
  [...]
 )
)

合并后的数组:

Array ( 
 [0] => Array ( [PointID] => -3621 [AddressID] => -503718 [CommonName] => XXXXX )
 [1] => Array ( [AddressID] => -503718 [LocationType] => 1
  [HouseNumber] => XXXX [AdditionalInfo] => [StreetPrefix] => N
  [StreetPreType] => [StreetName] => XXXX [StreetType] => AVE
  [StreetSuffix] => [Apt] => [XStreetPrefix] => [XStreetPreType] =>
  [XStreetName] => [XStreetType] => [XStreetSuffix] => [VenueName] => XXXX
  [City] => XXXXX [vsState] => 52 [Zip] => XXXX )
)

代码:

echo "<table>
<tr>
<th>CN</th>
<th>House#</th>
<th>St Name</th>
<th>St Prefix</th>
<th>St Type</th>
<th>City</th>
<th>Zip</th>
</tr>";
//Combine the arrays into one
$output = array_merge($names, $addressInfo);
print_r($output);
//Run a loop   
foreach ($output as $test)
    {  
    echo "<tr>";
    echo "<td>".$test['CommonName']."</td>";
    echo "<td>".$test['HouseNumber']."</td>";
    echo "<td>".$test['StreetName']."</td>";
    echo "<td>".$test['StreetPrefix']."</td>";
    echo "<td>".$test['StreetType']."</td>";
    echo "<td>".$test['City']."</td>";
    echo "<td>".$test['Zip']."</td>";
    echo "</tr>";
    }
echo "</table>";

2 个答案:

答案 0 :(得分:0)

显然,您的2个数组$names$addressInfo已包含其他级别的数据。

您可能想要这样做:

$output = array_merge($names[0], $addressInfo[0]);

如果不起作用,请在合并之前提供print_r()$names的{​​{1}}输出。

答案 1 :(得分:0)

好的,我正在创建另一个答案,以使其更清晰。

这是您当前的代码:

$cn = "AARONS"; //Connect to NWSDB

$pdo = new PDO("odbc:NWS","docserver","docserver");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$result = $pdo->prepare("SELECT CommonName,AddressID from CommonNameAddress where CommonName like :name");
$result->execute(array(':name' => "%$cn%"));
//Put the results in an array for use later in the table
$names = array();
$addquery = array();
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
    $addquery[] = $row['AddressID'];
    $names[] = $row;
}



$pdo = new PDO("odbc:NWS","docserver","docserver");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//Create an array for storing the address information from the below query
$addressInfo = array();
//Loop the query to get a result for every CommonName
foreach ($addquery as $address) {
    //Match the AddressID from the CommonNames Table to the Address information in the Address Table
    $common = $pdo->prepare("SELECT HouseNumber,StreetPrefix,StreetName,StreetType,City,Zip from Address where AddressID like :id");
    $common->execute(array(':id' => $address));
    //Put the results in an array for use later in the table
    while($row2 = $common->fetch(PDO::FETCH_ASSOC)) { $addressInfo[] = $row2; }
} 

以下是您可以做的事情:

$cn = "AARONS"; //Connect to NWSDB

$pdo = new PDO("odbc:NWS","docserver","docserver");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$result = $pdo->prepare("SELECT CommonName, CommonNameAddress.AddressID, Address.HouseNumber, Address.StreetPrefix, Address.StreetName, Address.StreetType, Address.City, Address.Zip from CommonNameAddress LEFT JOIN Address ON Address.AddressID=CommonNameAddress.AddressID where CommonName like :name");
$result->execute(array(':name' => "%$cn%"));
//Put the results in an array for use later in the table
$names = array();
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
    $names[] = $row;
}
print_r($names);
?>

告诉我们它是怎么回事。