PHP - 如何循环 2 组数组并将它们混合在一起?

问题描述 投票:0回答:1

我在循环数组并将它们混合为一组新数据时遇到问题。第一组数组是房间 ID。另一组是日期范围。我想每天在一个房间 id 数组中循环。

这是我的资源:

$_rid=array("5","6");
$date_range=getDays('2012-11-30','2012-12-05');
$_sid=md5(time());

获取两天之间的日期功能:

function getDays($strDateFrom,$strDateTo) {
  // takes two dates formatted as YYYY-MM-DD and creates an
  // inclusive array of the dates between the from and to dates.

  // could test validity of dates here but I am already doing
  // that in the main script

  $aryRange=array();

  $iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2),     substr($strDateFrom,8,2),substr($strDateFrom,0,4));
  $iDateTo=mktime(1,0,0,substr($strDateTo,5,2),     substr($strDateTo,8,2),substr($strDateTo,0,4));

  if ($iDateTo>=$iDateFrom) {
    array_push($aryRange,date('Y-m-d',$iDateFrom)); // first entry

    while ($iDateFrom<$iDateTo) {
      $iDateFrom+=86400; // add 24 hours
      array_push($aryRange,date('Y-m-d',$iDateFrom));
    }
  }
  return $aryRange;
}

所以我写道:

foreach($_POST[vid] as $_vidz){//5-6
    foreach($date_range as $val0){  
    //get cost from villas_rate each date
    $sql_rCost="select vr_cost from villas_rate where vr_id='$_vidz'";
    //echo $sql_rCost."<hr />";
    $result_rCost=mysql_db_query($dbname,$sql_rCost);
    while($rec_rCost=mysql_fetch_array($result_rCost)){
        $_rCostDBcost=explode("-",str_replace(",","",$rec_rCost['vr_cost']));   
            $_rate=$_rCostDBcost[$_rtype-1];//rate starts with 0
            $_date=$val0;
            $sql_cBk="insert into booking_customer values('','$_sid','$_vidz','$_rate','$_agc','$_date')";
            echo $sql_cBk."<br />";             
        }
    }
}

结果 从结果来看应该不错。但它只循环数组 $_rid=5 中的一个值。

insert into booking_customer values('','e1eb3f2e2c0fe99780c0354fa699a827','5','2012-11-30')
insert into booking_customer values('','e1eb3f2e2c0fe99780c0354fa699a827','5','2012-12-01')
insert into booking_customer values('','e1eb3f2e2c0fe99780c0354fa699a827','5','2012-12-02')
insert into booking_customer values('','e1eb3f2e2c0fe99780c0354fa699a827','5','2012-12-03')
insert into booking_customer values('','e1eb3f2e2c0fe99780c0354fa699a827','5','2012-12-04')
insert into booking_customer values('','e1eb3f2e2c0fe99780c0354fa699a827','5','2012-12-05')
php mysql arrays loops foreach
1个回答
0
投票

可能您在 HTML 表单中使用了像

<input name="vid" .. <input name="vid" ..
这样的输入名称,在这种情况下您会遇到麻烦:

foreach($_POST[vid] as $_vidz)

您应该使用带有

[]
的名称,例如
<input name="vid[]" .. <input name="vid[]" ..

villas_rate
中不存在带有
vr_id = 6

的记录
© www.soinside.com 2019 - 2024. All rights reserved.