duansha7025 2018-03-28 23:23
浏览 246
已采纳

将多维PHP数组转换为类似于表格的格式,并将键作为列

I have the following multidimensional array where the parent keys are long hashes, each of which contains a number of key=>value pairs which result in the following PHP data structure:

Array ( [riCQTaeQmczjR7ynRB30wzCghOhN3T82h9qXYl8M] => Array ( [Timestamp] => 2018-03-28 21:47:53 [What's your name?] => Alberto [When do you want to book] => tomorrow ) [wD5bb9lNTxmb3EftdenrO3UMJThMQ6mDhpKQFvjC] => Array ( [Timestamp] => 2018-03-28 21:48:10 [What's your name?] => AAA [When do you want to book] => next week ) [02tgD3iOH2pIyrku1m9uukwwFeISHis7C9TlMaPR] => Array ( [Timestamp] => 2018-03-28 23:30:16 [What's your name?] => Osvaldo [When do you want to book] => next week ) [aPjjcwhr2HeuFOaw3Jc0ijsf6C5VtAxOquSduIOP] => Array ( [Timestamp] => 2018-03-28 23:31:16 [What's your name?] => Jhonny [When do you want to book] => tomorrow ) )

The data structure above can be also visually represented as:

enter image description here

Notice that each one of the parent elements (i.e. long hashes) always contains the same keys as all the others (in this example each parent element contains 1 timestamp and 2 question-answer pairs).

My goal is to convert this multidimensional array into the following table-like format that can be easily exported in a csv file:

"Timestamp","What's your name?","When do you want to book?"
"2018-03-28 21:47:53","Lorna","tomorrow"
"2018-03-28 21:48:10","Jake","next week"
"2018-03-28 23:30:16","Sarah","next week"
"2018-03-28 23:31:16","Johnny","tomorrow"

The array keys become the "header", while each line represents one answer. I think one way to go would be using the PHP function array_map() but I wasn't able to achieve the desired result. Any idea / tip on how to do this?

  • 写回答

1条回答 默认 最新

  • dqan70724 2018-03-28 23:31
    关注
    <?php
    
    $data = [
        [
            'Timestamp' => '2018-03-01',
            'What\'s your name?' => 'Johnny',
            'When do you want to book?' => 'tomorrow'
        ],
        [
            'Timestamp' => '2018-03-01',
            'What\'s your name?' => 'Johnny',
            'When do you want to book?' => 'tomorrow'
        ],
        [
            'Timestamp' => '2018-03-01',
            'What\'s your name?' => 'Johnny',
            'When do you want to book?' => 'tomorrow'
        ],
    ];
    
    // get the headers
    $headers = array_keys(array_values($data)[0]);
    
    // open the file
    $resource = fopen('output.csv', 'w');
    
    // put the headers
    fputcsv($resource, $headers);
    
    // put the data
    foreach ($data as $d) {
        fputcsv($resource, $d);
    }
    
    // close the file
    fclose($resource);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?