我有这个多维数组,我想在excel文件中写这个数组,然后把它作为附件发送。
{
"data": [
{
"id": "77",
"m_id": "86",
"sub_id": null,
"s_id": "130",
"amt": "50.00",
"credit_amount": "0.00",
"amt_stamp": "-40.00",
"t_type": "Stock",
"t_type_sub": "Cash",
"t_mode": "Cr",
"t_desc": "",
"picture": null,
"short_url": null,
"send_sms": "0",
"post_time": "2017-09-19 15:51:56",
"bank": null,
"credit": null,
"sub_account": null,
"expense": null,
"expense_account": null
},
{
"id": "68",
"m_id": "86",
"sub_id": null,
"s_id": "130",
"amt": "100.00",
"credit_amount": "-1000.00",
"amt_stamp": "10.00",
"t_type": "Stock",
"t_type_sub": "Cash",
"t_mode": "Cr",
"t_desc": "checking SMS",
"picture": null,
"short_url": null,
"send_sms": "1",
"post_time": "2017-08-30 15:45:18",
"bank": null,
"credit": null,
"sub_account": null,
"expense": null,
"expense_account": null
}
]
}
我想在excel表中编写这个数组,然后在编写数组后,将其作为附件发送。 我想要这样的输出excel格式:
id m_id amt
21 2 4000
22 5 5000
答案 0 :(得分:0)
这应该让你去 - 最简单的可能是使用.csv文件类型。如果要切换行/列布局,可以在Excel中“转置”数据(复制/粘贴选项 - 转置)
<?php
$json = file_get_contents('./json_src.json');
$sourcedata = json_decode($json, true);
$handle = fopen("output.csv",'w+b') or die("problem opening csv file");
foreach($sourcedata as $key => $value)
{
foreach($value as $key => $value)
{
foreach($value as $key => $value)
{
// set the fields you'd like to copy to .csv
if($key==='id' || $key === 'm_id' || $key === 'amt')
{
$csv[]= array($key, $value);
}
}
}
}
foreach($csv as $key => $value)
{
fputcsv($handle, $value);
}
fclose($handle) or die("problem closing file");
?>