使用foreach插入数组的值

时间:2014-02-12 06:29:21

标签: php

我有一个数组$res_arr,我可以计算每个数组的最大值和最小值。

$res_arr= array(
    0=>array(1,2,3,6,7,8,9,10),
    1=>array(10,20,30,60,70,80,90,100),
   2=>array(12,12,13,16,17,18,19,10),
);

我有第二个数组在每个数组中都有一个值

$time_arr = array(
0=>array(12:20:30),
1=>array(12:20:35),
2=>array(12:20:40),
)
print_r($time_arr );

如何在$time_arr的foreach循环中插入$res_arr

我将其插入下表中的表格中。

  $b=0;
$maketemp = "CREATE   TABLE $table(";
  $maketemp .= " ID int(11) NOT NULL AUTO_INCREMENT, ";
  $maketemp .= " TIME varchar(10), ";
$maketemp .= " MIN varchar(10), ";
$maketemp .= " MAX varchar(10), ";
$maketemp .= " SPAN varchar(10), ";
$maketemp .= " PRIMARY KEY (ID), ";

foreach($res_arr as $sub)
{


   $tempMax = max($sub);  
   $tempMin = min($sub);  
   $span = $tempMax-$tempMin;
 //print_r($time_arr);
 mysql_query ("insert into $table (TIME,MIN,MAX,SPAN) values('timevalue','".$tempMax."','".$tempMin."','".$span."')");


$b++;


}

1 个答案:

答案 0 :(得分:0)

假设两个数组都具有相同数组键的相关值:

foreach($res_arr as $key => $sub) {
  $tempMax = max($sub);
  $tempMin = min($sub);
  $span = $tempMax-$tempMin;
  // get time value from other array using key
  $timeValue = $time_arr[$key];
  // ...