[英]Issue in iterating data and putting in csv file in php
I made this code 我写了这段代码
private function FileLog($parsedData)
{
$fp = fopen(dirname(__FILE__).'/logs/'.date('d-m-Y').'.csv','a+');
$HeaderKey = array();
foreach($parsedData as $key => $value)
{
echo $key."<br>";//$key has names of all parameters
foreach($value as $key1 => $value1)
{
echo $key1." :: ".$value1."<br>";//value1 has all the values
}
$HeaderKey[] = $key;
}
fputcsv($fp,$HeaderKey);
fclose($fp);
}
This is what the output is 这就是输出
StartTag
0 :: $$
1 :: $$
2 :: $$
3 :: $$
LastUpdateTimeInSec
0 :: 1539544229
1 :: 1539544293
2 :: 1539544505
3 :: 1539544548
NumberOfParams
0 :: 22
1 :: 22
2 :: 22
3 :: 22
As you can see $key1 give keys and $value1 give values ...What i want to do is to put all the values with same keys together. 如您所见, $ key1提供键,$ value1提供值 ...我想要做的是将所有具有相同键的值放在一起。 0 startTag value with 0 LastUpdateTimeInSec and with 0 NumberOfParams together and so on with 1,2 and 3. And finally put in csv file in that sequence. 0 startTag值和0 LastUpdateTimeInSec以及0 NumberOfParams一起,以此类推,分别是1,2和3。最后按该顺序放入csv文件。
Use array_map to combine data to output array 使用array_map将数据合并到输出数组
private function FileLog($parsedData)
{
$fp = fopen(dirname(__FILE__).'/logs/'.date('d-m-Y').'.csv','a+');
$output = array_map(null, $parsedData['StartTag'], $parsedData['LastUpdateTimeInSec'], $parsedData['NumberOfParams']);
foreach($output as $line) {
fputcsv($fp, $line);
}
fclose($fp);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.