PHP使用regex循环和查询mysql以输出excel文件

PHP使用regex循环和查询mysql以输出excel文件,php,mysql,regex,Php,Mysql,Regex,我做错了什么。我希望我们首先得到的数组被转置。然后正则表达式将其中一列拆分,以便我们可以查询数据库中的另一个表,用正确的标题替换该列标题。正则表达式正确吗?转置正确吗 这将得到我们想要的查询 exportMysqlToCsv($tablename,$tokenmain, $id); function exportMysqlToCsv($tablename,$tokenmain, $id, $filename = 'Results.csv'){ $sql_query = "select

我做错了什么。我希望我们首先得到的数组被转置。然后正则表达式将其中一列拆分,以便我们可以查询数据库中的另一个表,用正确的标题替换该列标题。正则表达式正确吗?转置正确吗

这将得到我们想要的查询

exportMysqlToCsv($tablename,$tokenmain, $id);
function exportMysqlToCsv($tablename,$tokenmain, $id, $filename = 'Results.csv'){
      $sql_query = "select * from $tablename where $tokenmain";

// Gets the data from the database

$result = mysql_query($sql_query);


$f = fopen('php://temp', 'wt');
$first = true;

$temp = mysql_fetch_array($result);
我希望这能转换我们刚得到的阵列。这里不太清楚

array_unshift($temp, null);
$transposed_array = call_user_func_array('array_map', $temp);
然后,我们通过转置数组循环替换接收到的列标题(例如111X2222X3333),并将这些标题拆分为查询要替换的标题,并将其输出到excel文件。这就是我非常困惑的地方

for ($i = 0; $i < $count; $i++) {
if ($transposed_array[$i][0])//starts with a number
{
    $sid = regexp /^[0-9]*/

    $gid = regexp /X[0-9]*X/
    //remove first and last character from $gid
    $gid = str_replace("X", "", $gid)

    $qid = regexp /[0-9]*$/

    $question_text = mysql_query("select question from lime_questions where sid =           ".$sid." and gid = ".$gid." and qid = ".$qid." limit 1");
    $transposed_array[$i][5] = $question_test
}
//恢复正常处理

$size = ftell($f);
rewind($f);
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Length: $size");
//输出到具有适当mime类型的浏览器,您可以选择;)


不确定您想要实现什么,因为以下内容不是有效的PHP表达式

  $sid = regexp /^[0-9]*/
  $gid = regexp /X[0-9]*X/
  $qid = regexp /[0-9]*$/
但是你的意见呢


我想让这个循环分割我们从第一个数组接收到的列标题。列标题的示例是111x222x333。我想使用正则表达式将它们分开,这样每个都有自己的属性,比如sid=111 gid=222和qid=3333

你所需要的只是

$string = "111X222X3333" ;
list($sid,$gid,$qid) = explode("X", $string);

你为什么把这里的事情搞复杂了???没有比`$sid=regexp/^[0-9]*/`或`$gid=regexp/X[0-9]*X/`更好的方法了,您能说说在($i=0;$i<$count;$i++)的循环
中想要实现什么吗{
然后我们可以帮助我希望这个循环分割我们从第一个数组收到的列标题。列标题的一个例子是111x222x333。我希望使用正则表达式将它们分割,这样每个都是它自己的属性,比如sid=111 gid=222和qid=3333,但是有100个字符串,比如111X2222X3333,所以我希望循环分解所有我想做的一种方法是,每当它找到一个以整数开头的头时,就做一个循环,只要把这行
列表($sid,$gid,$qid)=分解(“X”,$string);
放在循环中..只把
$string
替换为正在拆分的实际值
  $sid = regexp /^[0-9]*/
  $gid = regexp /X[0-9]*X/
  $qid = regexp /[0-9]*$/
$string = "111X222X3333" ;
list($sid,$gid,$qid) = explode("X", $string);