Flatten multidimensional assoc array (从JSON解码)

Flatten multidimensional assoc array (decoded from JSON)

我有 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 文件。

我希望你已经解决了这个问题,如果没有,我附上一些代码,让你有个想法,我所做的是,首先解码你得到的 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);

我做到了,这里有一个函数可以准确地解析它 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