- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我很难理解如何实现以下结果:
目前,基本上我能做的是输出一个带有转置数据的 CSV 文件,如上图左侧所示。我的问题是尝试获取也已转置的列名称,并与生成的查询数据位于同一行(我想要实现的在上图右侧演示)。
我的代码如下:
$output = fopen('php://output', 'w');
$headers = array();
$rows = array();
$row_names = array();
while($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
if(empty($headers)) {
$headers = array_keys($result);
// Transpose column names
foreach($headers as $key=>$val)
{
$row_names[$key][] = $val;
}
}
// Transpose column data
foreach($result as $key=>$val)
{
$rows[$key][] = $val;
}
}
// Prints the row names
foreach($row_names as $name)
{
fputcsv ($output, $name);
}
// Prints the row data
foreach($rows as $row)
{
fputcsv ($output, $row);
}
fclose($output);
exit;
我知道我可能必须以某种方式将 $rows 与 $row_names 结合起来,然后使用 fputcsv 执行单个 foreach,但我不确定如何完成此操作...
这是 $result 的输出:
Array
(
[Tail #] => N6232D
[Aircraft Configuration] => IR
[Flight Date] => 2015-11-05
[Pilot (Last Name)] => Mars
[Airport] => GA03
[City] => Ringgold
[State/Province] => GA
[Pre-Hobbs Time] => 41242
[Post-Hobbs Time] => 23423
[Hobbs Time] => -17819
[Pre-Tach Time] =>
[Post-Tach Time] =>
[Tach Time] =>
[Window Start Time] => 00:00:00
[Window Close Time] => 00:00:00
[Total Image Window] => 00:00:00
[On Station (Revenue)] => 00:00:00
[Weather] => 00:00:00
[ATC] => 00:00:00
[Pictometry Equip Failure/Delay] => 00:00:00
[Unscheduled Maintenance Time] => 00:00:00
[Scheduled Maintenance Time] => 00:00:00
[Pilot Delay] => 00:00:00
[Ground Support Delay] => 00:00:00
[Ferry] => 01:00:00
[Fueling/Lunch] => 00:00:00
[Plane - No Pilot] => 00:00:00
[Nothing Assigned] => 00:00:00
[Other/Miscellaneous] => 02:15:00
[Pilot Reported Times] => 03:15:00
[Completed Flight Plans (Include Project Name)] =>
[Partial Flight Plans] =>
[Date of Last Control Field] => 2015-10-05
[Drives Shipped Tracking Number] =>
[Spare Drive Sets] => -1
[Remarks] =>
[Status] => APPROVED
)
这是 $row_names 的输出片段:
Array
(
[0] => Array
(
[0] => Tail #
)
[1] => Array
(
[0] => Aircraft Configuration
)
[2] => Array
(
[0] => Flight Date
)
[3] => Array
(
[0] => Pilot (Last Name)
)
[4] => Array
(
[0] => Airport
)
[5] => Array
(
[0] => City
)
[6] => Array
(
[0] => State/Province
)
[7] => Array
(
[0] => Pre-Hobbs Time
)
[8] => Array
(
[0] => Post-Hobbs Time
)
[9] => Array
(
[0] => Hobbs Time
)
[10] => Array
(
[0] => Pre-Tach Time
)
[11] => Array
(
[0] => Post-Tach Time
)
[12] => Array
(
[0] => Tach Time
)
[13] => Array
(
[0] => Window Start Time
)
[14] => Array
(
[0] => Window Close Time
)
[15] => Array
(
[0] => Total Image Window
)
[16] => Array
(
[0] => On Station (Revenue)
)
[17] => Array
(
[0] => Weather
)
[18] => Array
(
[0] => ATC
)
[19] => Array
(
[0] => Pictometry Equip Failure/Delay
)
[20] => Array
(
[0] => Unscheduled Maintenance Time
)
[21] => Array
(
[0] => Scheduled Maintenance Time
)
[22] => Array
(
[0] => Pilot Delay
)
[23] => Array
(
[0] => Ground Support Delay
)
[24] => Array
(
[0] => Ferry
)
[25] => Array
(
[0] => Fueling/Lunch
)
[26] => Array
(
[0] => Plane - No Pilot
)
[27] => Array
(
[0] => Nothing Assigned
)
[28] => Array
(
[0] => Other/Miscellaneous
)
[29] => Array
(
[0] => Pilot Reported Times
)
[30] => Array
(
[0] => Completed Flight Plans (Include Project Name)
)
[31] => Array
(
[0] => Partial Flight Plans
)
[32] => Array
(
[0] => Date of Last Control Field
)
[33] => Array
(
[0] => Drives Shipped Tracking Number
)
[34] => Array
(
[0] => Spare Drive Sets
)
[35] => Array
(
[0] => Remarks
)
[36] => Array
(
[0] => Status
)
这是 $rows 的输出片段:
Array
(
[Tail #] => Array
(
[0] => N6232D
[1] => N6232D
[2] => N6232D
[3] => N6232D
[4] => N6232D
)
[Aircraft Configuration] => Array
(
[0] => IR
[1] => IR
[2] => IR
[3] => IR
[4] => IR
)
[Flight Date] => Array
(
[0] => 2015-11-01
[1] => 2015-11-05
[2] => 2015-11-06
[3] => 2015-11-08
[4] => 2015-11-09
)
[Pilot (Last Name)] => Array
(
[0] => Mars
[1] => Mars
[2] => Mars
[3] => Mars
[4] => Mars
)
[Airport] => Array
(
[0] => KROC
[1] => GA03
[2] => C37
[3] => GA03
[4] => PHNL
)
[City] => Array
(
[0] => Rochester
[1] => Ringgold
[2] => Brodhead
[3] => Ringgold
[4] => Honolulu
)
[State/Province] => Array
(
[0] => NY
[1] => GA
[2] => WI
[3] => GA
[4] => HI
)
[Pre-Hobbs Time] => Array
(
[0] => 12500
[1] => 41242
[2] => 15666.8
[3] => 15690
[4] => 15000
)
[Post-Hobbs Time] => Array
(
[0] => 12501.9
[1] => 23423
[2] => 15669.8
[3] => 15699.5
[4] => 15000.9
)
[Hobbs Time] => Array
(
[0] => 1.89999999999964
[1] => -17819
[2] => 3
[3] => 9.5
[4] => 0.899999999999636
)
[Pre-Tach Time] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
)
[Post-Tach Time] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
)
[Tach Time] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
)
[Window Start Time] => Array
(
[0] => 12:35:00
[1] => 00:00:00
[2] => 15:30:00
[3] => 15:30:00
[4] => 12:30:00
)
[Window Close Time] => Array
(
[0] => 15:40:00
[1] => 00:00:00
[2] => 17:15:00
[3] => 17:15:00
[4] => 15:45:00
)
[Total Image Window] => Array
(
[0] => 03:05:00
[1] => 00:00:00
[2] => 01:45:00
[3] => 01:45:00
[4] => 03:15:00
)
[On Station (Revenue)] => Array
(
[0] =>
[1] => 00:00:00
[2] => 00:50:00
[3] => 00:00:00
[4] => 00:00:00
)
最佳答案
使用带有索引计数器的简单 for
循环怎么样,前提是两个数组的大小相同。并使用 array_merge
将 header 名称和值合并到一行中。
// Prints the row names and the values in one row.
$count_rows = count($rows);
for($i=0; $i<$count_rows; $i++)
{
fputcsv ($output, array_merge($row_names[$i], $rows[$i]));
}
** 编辑 **
根据新信息,您可以像这样更改代码的开头
while($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
if(empty($headers)) {
$headers = array_keys($result);
// Transpose column names
foreach($headers as $key=>$val)
{
$rows[$key][] = $val;
}
}
// Transpose column data
foreach($result as $key=>$val)
{
$rows[$key][] = $val;
}
}
请注意,标题名称和值现在都存储在同一个数组中,但由于标题始终排在第一位,因此它始终成为每行的第一个值,这正是您想要的。
现在我们可以只对行使用一个foreach
。
// Prints the row data
foreach($rows as $row)
{
fputcsv ($output, $row);
}
关于PHP - 转置数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33637777/
我有一个“设置首选项”屏幕。它有一个 ListPreference 和一个 CheckBoxPreference。当我选择 ListPreference 的一项时,我想更改应用程序的日期格式。另外,通
我试图找到创 build 置/配置窗口的示例。单击菜单项中的“选项”操作可启动设置窗口。我想弄清楚如何从主窗口打开第二个窗口。以及新窗口如何将设置信息返回主窗口。尝试使用 QDialog 或一些继承的
我在 Lnux 上有 Qt 应用程序。我想为此创建一个可执行文件/设置以便在 Windows 上分发它并且不需要安装 Qt。我通过包含所有 dll 为此创建了可执行文件但要运行它,用户需要进入文件夹。
我正在尝试创建一个有点动态的 html 类,它根据类末尾包含的数字设置宽度 %。注意:类名将始终以“gallery-item-”开头 示例:div.gallery-item-20 = 20% 宽度 我
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 6 年前。 Improve this qu
在我的应用程序中,我想记住一些变量,例如,如果用户登录过一次,那么他们将在下次重新打开应用程序时登录,或者如果他们决定禁用某些提醒,应用程序可以检查该变量是否是错误的,将不再显示该提醒。理想情况下,这
我在 Netbeans 中开发了一个应用程序,它连接到远程计算机的消息队列并发送消息。该应用程序还有其他功能。项目完成后,我清理并构建应用程序,然后 Netbeans 创建一个 jar 文件。 但我的
我创建了一个 Outlook 加载项,需要创建一个设置以使其可分发(我是新手,所以请原谅新手评论) Outlook -2010 Vs -2010 .Net 4.0 我读了一些地方,最简单的方法就是发
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: How to make installer pack of Java swing Application Proje
这个问题肯定已经被很多人解决过很多次了,但是经过几个小时的研究,我仍然没有找到我要找的东西。 我有一个 ExportSettings.settings 文件,其中包含一堆设置( bool 值、字符串、
我想为我的项目创建一个安装程序,以便它可以安装在任何电脑上而无需安装头文件。我怎样才能做到这一点? 最佳答案 一般有两种分发程序的方法: 源代码分发(要构建的源代码)。最常见的方法是使用 GNU au
如何在这样的动态壁纸中创 build 置 Activity ? Example Picture 我只用一个简单的文本构建了设置 Activity ,但遇到了一些问题。第一个问题是我不能为此 Activ
我用 GUI 创建了一个简单的软件。它有几个源文件。我可以在我的编辑器中运行该项目。我认为它已经为 1.0 版本做好了准备。但我不知道如何为我的软件创 build 置/安装程序。 源代码是python
我的 SettingsActivity当前扩展了 Android Studio 生成的类,AppCompatPreferenceActivity扩展 PreferenceActivity . Acti
我正在使用 .NET 为 IE 开发工具栏。目前,我使用 gacutil 插入我的 .NET 程序集,并使用 regasm 注册我的 COM 程序集。 我想为项目创建一个设置 (MSI),但我似乎无法
在为设置页面创建 Activity 后,我注意到 if (mCurrentValue !== value) 中的 mCurrentValue !== value 返回警告: Identity equa
我在 Visual Studio 10 中创建了一个项目,该项目使用 Mysql 数据库和 Crystalreports 以及 它。但是我不知道如何进行自动安装 Mysql 和 Crystalrepo
我正在尝试在我的 C# 项目中使用 Sqlite 数据库,并且我在 IDE 中做得很好。我的问题是当我为我的项目制作安装包并安装它时,程序无法访问 sqlite 数据库。我也知道这是因为用户没有访问文
我有一个大型 Web 应用程序(带有 11 子系统的 ErP),我想使用 Microsoft WebPI 为它创建一个设置。 目前,我们每周向客户发送一次应用程序(用于每周更新)。 我们在此应用程序中
所以我对工资单申请的最终查询是 - 如何为薪资申请创 build 置? 我需要知道的一切- 如何将设置项目添加到我现有的解决方案 如何将解决方案中的文件添加到安装项目中,以及添加哪些文件添加和在什么文
我是一名优秀的程序员,十分优秀!