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

时间:2018-02-02 22:07:21

标签: php arrays json csv flatten

我有这样的JSON对象:

{
  "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"
    }
    ]
    }
  ]
}

我想将此对象写入CSV文件,但具有特定格式。我希望得到的结果显示在这个网站上:https://json-csv.com/ 只需复制我的JSON即可查看,这里是快速截图:http://take.ms/p2FAI

问题是如何正确地重构数组以获得此输出:

$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",
        ""
    )
);

任何想法:c?我完全不能正确地压扁阵列,所以我可以把它放到CSV文件中。

2 个答案:

答案 0 :(得分:0)

我希望你已经解决了这个问题,如果不是我附加一些代码,让你知道,我做了什么,首先解码你得到的json输出,然后,读取输出并进行深入需要,使用关联数组来平整你的响应,代码不完整,但它工作,会让你开始,希望你运气好!

$jsonvalue = json_decode('{
  "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"
        }
      ]
    }]
}');
    $flatArray = [];
    $chatID = 0;
    foreach ($jsonvalue as $chats) {
        foreach ($chats as $chatProperty) {
            $flatArray[$chatID] = $chatID;
            $flatArray['chatType'] = $chatProperty->type;
            //visitor
            $flatArray['visitorID'] = $chatProperty->visitor->id;
            $flatArray['visitorName'] = $chatProperty->visitor->name;

            foreach ($chatProperty->group as $groupValue) {
                $flatArray['group_' . $groupValue] = $groupValue;
                echo $groupValue;
            }

            foreach ($chatProperty->messages as $value) {
                $flatArray['author_name'] = $value->author_name;
                $flatArray['text'] = $value->text;
                $flatArray['welcome_message'] = isset($value->welcome_message) ?
                    $value->welcome_message : 'no welcome message';
            }
            $chatID++;
        }
    }

    var_dump($flatArray);

答案 1 :(得分:0)

我做到了,这里的函数完全按照https://json-csv.com/的方式解析它。

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;
}

谢谢你们的帮助< 3