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

将多维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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?