我從數據庫中的兩個不同表中提取數據。一旦我擁有了我所需要的全部內容,我想要將這些數組合並打印到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>";
該死!這很好!非常感謝soooo!我終於用自己的for循環來想出它,但是你的方法效率提高了十億倍,所以我就這麼做了!再次感謝! –
Coool :-)請加我的答案。祝你的項目好運。 – Thibault