将多维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:
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?
我有以下多维数组,其中父键是长哈希,每个哈希包含一些键=&gt ;值对导致以下PHP数据结构: p>
数组([riCQTaeQmczjR7ynRB30wzCghOhN3T82h9qXYl8M] =>数组([时间戳] => 2018-03-28 21 :47:53 [你的名字是什么?] => Alberto [你想什么时候预订] =>明天)[wD5bb9lNTxmb3EftdenrO3UMJThMQ6mDhpKQFvjC] =>数组([时间戳] => 2018-03-28 21:48: 10 [你的名字是什么?] => AAA [你想什么时候预订] =>下周)[02tgD3iOH2pIyrku1m9uukwwFeISHis7C9TlMaPR] =>数组([时间戳] => 2018-03-28 23:30:16 [ 你的名字是什么?] => Osvaldo [你想什么时候预订] =>下周)[aPjjcwhr2HeuFOaw3Jc0ijsf6C5VtAxOquSduIOP] =>数组([时间戳] => 2018-03-28 23:31:16 [你的是什么] name?] => Jhonny [你想什么时候预订] =>明天)) p> blockquote>
数据str 上面的结构也可以直观地表示为: p>
请注意每一位父母 元素(即 long hashes)总是包含与所有其他键相同的键(在本例中,每个父元素包含1个时间戳和2个问题 - 答案对)。 p>
我的目标是将此多维数组转换为 可以在csv文件中轻松导出的以下类似于表的格式: p>
“Timestamp”,“你叫什么名字?”,“你想什么时候预订?” “2018-03-28 21:47:53”,“Lorna”,“明天” “2018-03-28 21:48:10”,“杰克”,“下周” “2018- 03-28 23:30:16“,”莎拉“,”下周“ ”2018-03-28 23:31:16“,”约翰尼“,”明天“ code> pre>
数组键成为“标题”,而每一行代表一个答案。 我认为一种方法是使用PHP函数
array_map() code>但我不是' 能够达到预期的效果。 关于如何做到这一点的任何想法/提示? p> div>
<?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);