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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!