dtz55359 2015-12-22 21:30
浏览 46
已采纳

COMPLEX PARSE.com问题! 查询数据并使用多维数组导出为CSV

I am having issues getting an array into a CSV file, I can get the output into the CSV file now, however it is only printing the following into the CSV file:

objectId    createdAt
Array   Array
Array   Array
Array   Array
Array   Array
Array   Array
Array   Array
Array   Array

I believe it is because I need to flatten the array, but am at a loss on how to do it, I am pretty much picking up how to work with arrays & parse.com as I go, I do however have good knowledge of PHP, My code is as follows:

$query = new ParseQuery("Counts");

 $results = $query->find();
 for ($i = 0; $i < count($results); $i++) 

 {    
    $detail=array();
    $object = $results[$i];

    $detail['objectId'][$i] = $object->getObjectId();
    $detail['createdAt'][$i] = $object->getCreatedAt();
  //  print_r($detail);
    $response[]=$detail;

 }


$output = fopen('php://output', 'w');
header("Content-Type:application/csv"); 
header("Content-Disposition:attachment;filename=dboutput.csv"); 
fputcsv($output, array('objectId','createdAt'));
foreach ($response as $fields) {
    fputcsv($output, $detail);
}
fclose($output);

instead of it outputting Array I need it to output the stored information, which is stored in my parse.com DB (createdAt = date & objectId = string).

If it helps the array looks like the following:

Array ( [date] => Array ( [0] => eGocH6OznC )

[createdAt] => Array
    (
        [0] => DateTime Object
            (
                [date] => 2015-12-11 23:02:03.968000
                [timezone_type] => 2
                [timezone] => Z
            )

    )

) Array ( [date] => Array ( [1] => P3uhKFzpsq )

[createdAt] => Array
    (
        [1] => DateTime Object
            (
                [date] => 2015-12-11 23:16:55.633000
                [timezone_type] => 2
                [timezone] => Z
            )

    )

)

I need to get just the Date value & objectId value into 2 columns of a CSV file.

Any guidance on this is greatly appreciated.

  • 写回答

1条回答 默认 最新

  • douchongbc72264 2016-01-02 11:28
    关注

    The correct code for doing this is:

    $query = new ParseQuery("Counts");
    
    $results = $query->find();
    $detail=array();
    for ($i = 0; $i < count($results); $i++)
    
    {
    
    $object = $results[$i];
    
    $detail[$i]['objectId'] = $object->getObjectId();
    $detail[$i]['createdAt'] = $object->getCreatedAt()->format('Y-m-d H:i:s');
    
    }
    
    
    $output = fopen('php://output', 'w');
    header("Content-Type:application/csv");
    header("Content-Disposition:attachment;filename=dboutput.csv");
    fputcsv($output, array('objectId','createdAt'));
    foreach ($detail as $fields) {
    fputcsv($output, $fields);
    }
    fclose($output);
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误