I have JSON object like this:
{
"chats": [
{
"type": "chat",
"visitor": {
"id": "S1506042519.608fc390eb",
"name": "Visitor"
},
"agents": [
{
"display_name": "Agent",
"email": "agent@example.com"
}
],
"chat_start_url": "https://example.com/",
"group": [
3, 4
],
"messages": [
{
"author_name": "Agent",
"text": "Hello. How may I help you?",
"welcome_message": true
},
{
"author_name": "Visitor",
"text": "transfer me to agent"
},
{
"author_name": "Agent",
"text": "Hello. How may I help you?"
},
{
"author_name": "Visitor",
"text": "transfer me to agent"
}
]
},
{
"type": "chat",
"visitor": {
"id": "S1506042519.608fc390eb",
"name": "Visitor"
},
"agents": [
{
"display_name": "Agent",
"email": "agent@example.com"
}
],
"chat_start_url": "https://example.com/",
"group": [
3, 4
],
"messages": [
{
"author_name": "Agent",
"text": "Hello. How may I help you?",
"welcome_message": true
},
{
"author_name": "Visitor",
"text": "transfer me to agent"
}
]
}
]
}
I want to write this object to CSV file, but with specific formatting. What I want to have as result is showed on this website: https://json-csv.com/ Just copy my JSON to see it, here is quick screenshot: http://take.ms/p2FAI
The question is how to properly refactor the array to get this output:
$refactoredArray = array(
0 => array(
"chats_type",
"chats_visitor_id",
"chats_visitor_name",
"chats_agents_display_name",
"chats_agents_name",
"chats_start_url",
"chats_group_1",
"chats_group_2",
"chats_messages_author_name",
"chats_messages_text",
"chats_messages_welcome_message"
),
1 => array(
"chat",
"S1506042519.608fc390eb",
"Visitor",
"Agent",
"agent@example.com",
"https://example.com/",
3,
4,
"Agent",
"Hello. How may I help you?",
true
),
2 => array(
"",
"",
"",
"",
"",
"",
"",
"",
"Visitor",
"transfer me to agent",
""
),
3 => array(
"",
"",
"",
"",
"",
"",
"",
"",
"Agent",
"Hello. How may I help you?",
""
),
4 => array(
"",
"",
"",
"",
"",
"",
"",
"",
"Visitor",
"transfer me to agent",
""
),
5 => array(
"chat",
"S1506042519.608fc390eb",
"Visitor",
"Agent",
"agent@example.com",
"https://example.com/",
3,
4,
"Agent",
"Hello. How may I help you?",
true
),
6 => array(
"",
"",
"",
"",
"",
"",
"",
"",
"Visitor",
"transfer me to agent",
""
)
);
Any ideas :c ? I totally can't flatten the array correctly so I could put it to CSV file.