这是一个关于@Blaatpraat提供的解决方案的问题,它解决了我的问题。
我现在有一个包含以下内容的数组:
Array ( [NXLHR01011474021550] =>
Array ( [UniqueID] => NXLHR01011474021550 [Room] => 0101 [AuditBy] => navexdemo2 [AuditDate] => 2016-09-16 11:26:00 [SeqID] => SeqID1306 [Status] => 1 [WaterHot] => 67.0 [WaterCold] => 18.0 )
[NXLHR01021474021587] =>
Array ( [UniqueID] => NXLHR01021474021587 [Room] => 0102 [AuditBy] => navexdemo2 [AuditDate] => 2016-09-16 11:26:00 [SeqID] => SeqID1306 [Status] => 0 [WaterHot] => 65.0 [WaterCold] => 21.0 )
)
我现在要逐行显示数组中的每条记录。
我的代码是;
foreach ($array as $key => $item) {
echo $array[$row['UniqueID']]['Room']."\n";
echo $array[$row['UniqueID']]['AuditBy']."\n";
echo $array[$row['UniqueID']]['WaterHot']."\n";
echo $array[$row['UniqueID']]['WaterCold']."\n";
}
但结果是: " 0102 navexdemo2 65.0 21.0" " 0102 navexdemo2 65.0 21.0" 两次相同的记录。
有谁知道为什么不显示数组中的第一条记录。
我得到了很多成员的帮助,我试图避免寻求帮助,但我已经尝试了很多方法让这个工作没有成功。
再一次,我感谢你的时间。
答案 0 :(得分:2)
这可能是你想要的吗?
<?php
$array = [
'NXLHR01011474021550' => [
'UniqueID' => 'NXLHR01011474021550',
'Room' => '0101',
'AuditBy' => 'navexdemo2',
'AuditDate' => '2016-09-16 11:26:00',
'SeqID' => 'SeqID1306',
'Status' => 1,
'WaterHot' => 67.0,
'WaterCold' => 18.0,
],
'NXLHR01021474021587' => [
'UniqueID' => 'NXLHR01021474021587',
'Room' => '0102',
'AuditBy' => 'navexdemo2',
'AuditDate' => '2016-09-16 11:26:00',
'SeqID' => 'SeqID1306',
'Status' => 0,
'WaterHot' => 65.0,
'WaterCold' => 21.0,
],
];
foreach($array as $uKey=>$arrData){
echo $arrData['Room']."\n";
echo $arrData['AuditBy']."\n";
echo $arrData['WaterHot']."\n";
echo $arrData['WaterCold']."\n";
}
要可视化输出更清晰,您可以采取以下路线:
<?php
$dataList = "<ul>" . PHP_EOL;
foreach($array as $uKey=>$arrData){
$sp = " ";
$dataList .= "<li><h3>Unique ID: {$uKey}</h3></li>" . PHP_EOL;
$dataList .= "<li><strong>Room: </strong>{$sp}{$arrData['Room']}</li>" . PHP_EOL;
$dataList .= "<li><strong>AuditBy: </strong>{$sp}{$arrData['AuditBy']}</li>" . PHP_EOL;
$dataList .= "<li><strong>WaterHot: </strong>{$sp}{$arrData['WaterHot']}</li>" . PHP_EOL;
$dataList .= "<li><strong>WaterCold: </strong>{$sp}{$arrData['WaterCold']}</li>". PHP_EOL;
}
$dataList .= "</ul>";
echo $dataList;
上面的echo $dataList
会产生如下内容:
答案 1 :(得分:0)
试试这个
foreach ($array as $key => $item) {
echo $item[$row['UniqueID']]['Room']."\n";
echo $item[$row['UniqueID']]['AuditBy']."\n";
echo $item[$row['UniqueID']]['WaterHot']."\n";
echo $item[$row['UniqueID']]['WaterCold']."\n";
}