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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器