douyi4912 2013-04-16 20:15
浏览 67
已采纳

如何在for循环中将另一行添加到PHP中的数组中

Hey guys I have the following:

for($i; $i< count($nontimedif); $i++){      
    $data[$i] = array(
      "Driver Chose Non-Compliance:" => $nontimedif[$i] . ", 
      " . $nonfirstname[$i]. ", 
      " . $nonlastname[$i]. ", 
      " . $nonemail[$i]. ", 
      " . .......
    );
}

so after this runs I want to add a few new rows because this than transfers into a .csv file. So how do I go about adding at the end of this?

How I am making the .csv file:

function cleanData(&$str)
  {
   // escape tab characters
    $str = preg_replace("/\t/", "\\t", $str);

    // escape new lines
    $str = preg_replace("/?
/", "\
", $str);

    // convert 't' and 'f' to boolean values
    if($str == 't') $str = 'TRUE';
    if($str == 'f') $str = 'FALSE';

    // force certain number/date formats to be imported as strings
    if(preg_match("/^0/", $str) || preg_match("/^\+?\d{8,}$/", $str) || preg_match("/^\d{4}.\d{1,2}.\d{1,2}/", $str)) {
      $str = "'$str";
    }

    // escape fields that include double quotes
    if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';

  }

// filename for download
  $filename = "website_data_" . date('Ymd') . ".xls";

  header("Content-Disposition: attachment; filename=\"$filename\"");
  header("Content-Type: application/vnd.ms-excel");

  $flag = false;
  foreach($data as $row) {
    if(!$flag) {
      // display field/column names as first row
      echo implode("\t", array_keys($row)) . "
";
      $flag = true;
    }
     array_walk($row, 'cleanData');
    echo implode("\t", array_values($row)) . "
";
  }
  • 写回答

3条回答 默认 最新

  • duanci5913 2013-04-16 20:16
    关注
    $data[] = array( 'Key:' => 'Whatever you want to add');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码