我想将我的 mysql 数据库的部分(并且排列方式不同)保存在 CSV 文件中。为此,我编写了这段代码:
$headerDisplayed = false;
foreach ($arr as &$currName)
{
$result2 = mysql_query("SELECT * FROM Experiment Where Person='$currName'");
$i = 1;
while ($row2 = mysql_fetch_array($result2))
{
$list['Name'] = $currName;
$list['Studie'] = $row2['Studie'];
if(strcmp($row2['Antwort'],$row2['Korrekt']) == 0)
{
$list["A $i"] = '0';
}
else
{
$list["A $i"] = '1';
}
$i++;
}
if ( !$headerDisplayed ) {
// Use the keys from $data as the titles
fputcsv($fp, array_keys($list));
$headerDisplayed = true;
}
fputcsv($fp, $list);
}
fclose($fp);
不幸的是,这不是我想要的。我的计划是$list
(例如$list["A 1"]
或$list["A 15"]
)的每个条目都有自己的列。然后调用应该为每个条目fputcsv
创建一个新行和一列。正如预期的那样,$list
我每次调用都会创建新行,但完整的数组位于第一列(最左边)。fputcsv
$list
我该怎么做才能解决这个问题?
补充: 我得到类似的东西:
[1,0,0,0,1,1,0,0] [] [] [] [] [] [] []
[1,0,1,1,0,1,0,0] [] [] [] [] [] [] []
[0,1,1,1,1,1,0,1] [] [] [] [] [] [] []
但我想要的更像是:
[1] [0] [0] [0] [1] [1] [0] [0]
[1] [0] [1] [1] [0] [1] [0] [0]
[0] [1] [1] [1] [1] [1] [0] [1]