douliangpo0128 2018-02-02 22:07
浏览 121
已采纳

展平多维关联数组(从JSON解码)

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.

  • 写回答

2条回答 默认 最新

  • doutinghou6980 2018-02-09 17:47
    关注

    I made it, here is function that parse it exactly how https://json-csv.com/ does it.

    function json2array($array) {
    
        $flat = array();
        foreach ($array as $key => $value) {
            if(is_array($value)) {
                $chatID = 0;
                foreach ($value as $chatsKey => $chatsValue) {
                    foreach ($chatsValue as $singleChatKey => $singleChatValue) {
                        if(is_array($singleChatValue)) {
                            $chatPropID = 0;
                            foreach ($singleChatValue as $chatPropKey => $chatPropValue) {
                                $maxPropID = 0;
                                if ($chatPropKey > $maxPropID)
                                    $maxPropID = $chatPropKey;
                                if(is_array($chatPropValue)) {
                                    foreach ($chatPropValue as $lastPropKey => $lastPropValue) {
                                        $flat[ $chatID+$chatPropKey ][ $key . '_' . $singleChatKey . '_' . $lastPropKey ] = $lastPropValue;
                                    }
                                } else {
                                    $flat[ $chatID ][ $key . '_' . $singleChatKey . '_' . $chatPropKey ] = $chatPropValue;
                                }
    
                                $chatPropID++;
                            }
                        } else {
                            $flat[ $chatID ][ $key . '_' . $singleChatKey ] = $singleChatValue;
                        }
                    }
                    $chatID += $maxPropID + 2;
                }
            } else
                $flat[0][$key] = $value;
        }
    
        return $flat;
    }
    

    Thank you guys for help <3

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?