duannan3959 2018-04-27 10:17
浏览 48
已采纳

如何格式化必须写在csv文件Codeigniter上的数据

I need help writing my data in a csv file in a particular format. I have json data as follows

[ { "name": "Abc", "class": "12", "date": "14-04-2018", "answers": "question1 : answer1, q2 : a2, q3 : a3, q4 : a4" } ]

I am storing the decoded version of this json in $details array. I have to write this data in a csv file. Here is my current code-

 $filename = 'logs_'.$id.'.csv'; 
 header("Content-Description: File Transfer"); 
 header("Content-Disposition: attachment; filename=$filename"); 
 header("Content-Type: application/csv; ");
 $file = fopen('php://output', 'w');
 $header = array("name","class","date","answers"); 
 fputcsv($file, $header);
 foreach ($details as $key=>$line){ 
    fputcsv($file,$line); 
 }
 fclose($file);
 exit;

I need this data in a particular format. I have attached the image containing both the current output and the required output. I want the answers to be written in separate rows as shown in the image.

Current and required format

(I have asked a similar question before but I had to delete it since it was ill-formed so please do not flag it) I want to achieve this without using any external libraries or pluggins. SO if you have any idea on how to achieve this then please let me know. Also, i was advised by one of the users on stackoverflow to use control break but i couldn't figure out a solution. If anyone else thinks thats the solution then please guide me

  • 写回答

1条回答 默认 最新

  • douwuli4512 2018-04-27 10:41
    关注

    the only way i can imagine is to restructure the array

    something like that should work

    $str = '[
        {
            "name": "Abc",
            "class": "12",
            "date": "14-04-2018",
            "answers": "question1 : answer1, q2 : a2, q3 : a3, q4 : a4"
        },
        {
            "name": "Abc",
            "class": "12",
            "date": "14-04-2018",
            "answers": "question1 : answer1, q2 : a2, q3 : a3, q4 : a4"
        }
    ]';
    
    $arrData = [];
    $arrJsonData = json_decode($str);
    
    foreach($arrJsonData AS $objItem)
    {
        $arrAnswers = explode(',', $objItem->answers);
        $objItem->answers = (count($arrAnswers) > 0)    ?   array_shift($arrAnswers)    :   array();
    
        $arrData[] = $objItem;
    
        if (count($arrAnswers) > 0)
        {
            foreach($arrAnswers AS $val)
            {
                $objNew = new stdClass();
                $objNew->name = '';
                $objNew->class = '';
                $objNew->date = '';
                $objNew->answers = $val;
    
                $arrData[] = $objNew;
    
            }
        }
    }
    
    print_r($arrData);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭