douyan2680 2016-09-11 20:11
浏览 137

合并两个数组并循环遍历它们以返回所有元素

I have two queries that return 5 rows of data from each query. I then merge the resulting arrays to create one array.

If I were using just one arry I would do it like this:

mysql_select_db($database_iMaint, $iMaint);
$query_WaterHot = sprintf("SELECT h.UniqueID, h.Room, h.AuditBy,  h.AuditDate, h.SeqID, h.WaterHot 
FROM NXLHR_Hist h 
WHERE (h.WaterHot IS NOT NULL OR
       h.WaterHot IS NOT NULL
      ) 
      AND 
      h.CompStamp >=  DATE('".$_POST['FromDate']."') 
      AND 
      h.CompStamp <= DATE('".$_POST['ToDate']."') 
ORDER BY h.Room ASC 
");
$WaterHot = mysql_query($query_WaterHot, $iMaint) or die(mysql_error());
$row_WaterHot = mysql_fetch_assoc($WaterHot);
$totalRows_WaterHot= mysql_num_rows($WaterHot);


do {

    echo $row_WaterHot['Room'];
    echo $row_WaterHot['WaterHot'];
    echo $row_WaterHot['WaterCold'];

} while ($row_WaterHot = mysql_fetch_assoc($WaterHot));

Above is how I would normally process the data but in this case I am using the results from two queries which is why I am merging them.

So I now need to loop through the merged array and output each of the values.

$row_WaterHot = mysql_fetch_assoc($Waterhot); // five records
$row_WaterCold = mysql_fetch_assoc($WaterCold); // five records
$Water = array();
$Water = array_merge($row_WaterHot, $row_WaterCold);

foreach ($Water as $key => $value) {

// how do I echo the results of each value like this
echo "the name of the $key and the $value

} 

Or should I be doing this another way?

Thanks in advance for your help and time.

  • 写回答

1条回答 默认 最新

  • dougong1031 2016-09-11 20:14
    关注

    Simply use a foreach This is pretty easy to use. The template looks like this :

    foreach($array as $key=>$value){
    }
    

    In your case, you would have something like this

      foreach($Water as $key=>$val)
            echo $val;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值