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?