如何从多维数组中删除特定的键

时间:2018-07-10 13:57:04

标签: php arrays

这就是我需要得出的结果

"data": [
        {
        "ticket_category_id": "677",
        "ticket_category_name": " Testing 500",
        "ticket_category_order": "1",
        "tickets": 
                [
                {
                "ticket_id": "927",
                "ticket_title": "EDI Project Template with attachments",
                "ticket_order": "1",
                "due_date": "0000-00-00 00:00:00",
                "created_date": "2018-05-16 10:01:04",
                "edited_date": "2018-05-17 02:56:38",
                "updated_date": "2018-05-17 02:56:38",
                "is_complete": "0",
                "total_comment": "0",
                "total_attachment": "10",
                "total_checklist_items": "48",
                "total_completed_checklist_items": "0",
                "label": [
                         {
                        "ticket_label_name": "",
                        "ticket_color_code": "#FF3B30"
                    },
                    "#D4891C": {
                        "ticket_label_name": "IN PROGRESS",
                        "ticket_color_code": "#D4891C"
                    }]
                },
                "people": []
            },
            {
                "ticket_id": "928",
                "ticket_title": "EDI Project Template",
                "ticket_order": "2",
                "due_date": "0000-00-00 00:00:00",
                "created_date": "2018-05-16 10:01:04",
                "edited_date": "0000-00-00 00:00:00",
                "updated_date": "0000-00-00 00:00:00",
                "is_complete": "0",
                "total_comment": "0",
                "total_attachment": "0",
                "total_checklist_items": "48",
                "total_completed_checklist_items": "0",
                "label": [
                         {
                        "ticket_label_name": "",
                        "ticket_color_code": "#4CD964"
                    }]
                },
                "people": []
            }
           ]

但是我得到的结果如下所示

 {
    "data": [
        {
            "ticket_category_id": "677",
            "ticket_category_name": " Testing 500",
            "ticket_category_order": "1",
            "tickets": {
                "927": {
                    "ticket_id": "927",
                    "ticket_title": "EDI Project Template with attachments",
                    "ticket_order": "1",
                    "due_date": "0000-00-00 00:00:00",
                    "created_date": "2018-05-16 10:01:04",
                    "edited_date": "2018-05-17 02:56:38",
                    "updated_date": "2018-05-17 02:56:38",
                    "is_complete": "0",
                    "total_comment": "0",
                    "total_attachment": "10",
                    "total_checklist_items": "48",
                    "total_completed_checklist_items": "0",
                    "label": {
                        "#FF3B30": {
                            "ticket_label_name": "",
                            "ticket_color_code": "#FF3B30"
                        }
                    },
                    "people": []
                }
            }
        },
        {
            "ticket_category_id": "677",
            "ticket_category_name": " Testing 500",
            "ticket_category_order": "1",
            "tickets": {
                "927": {
                    "ticket_id": "927",
                    "ticket_title": "EDI Project Template with attachments",
                    "ticket_order": "1",
                    "due_date": "0000-00-00 00:00:00",
                    "created_date": "2018-05-16 10:01:04",
                    "edited_date": "2018-05-17 02:56:38",
                    "updated_date": "2018-05-17 02:56:38",
                    "is_complete": "0",
                    "total_comment": "0",
                    "total_attachment": "10",
                    "total_checklist_items": "48",
                    "total_completed_checklist_items": "0",
                    "label": {
                        "#D4891C": {
                            "ticket_label_name": "IN PROGRESS",
                            "ticket_color_code": "#D4891C"
                        }
                    },
                    "people": []
                }
            }
        },

下面是我做的代码,我使用php mysql一对多关系将所有数据转换为平面数组,然后在下面的代码中将单个平面数组转换为三个多维数组,但是遇到了瓶颈。请帮助检查我的代码,以指出需要更改的部分才能达到预期的效果。

  $result = array();
    foreach($total_count as $Key => $Value){
        $ticketCatId = $Value['ticket_category_id'];
        $ticketId = $Value['ticket_id'];
        $ticketColorCode = $Value['ticket_color_code'];
        $ticketMember = $Value['user_id'];

        $result[$ticketCatId]['ticket_category_id'] = $Value['ticket_category_id'];
        $result[$ticketCatId]['ticket_category_name'] = $Value['ticket_category_name'];
        $result[$ticketCatId]['ticket_category_order']  = $Value['ticket_category_order'];

        if(!isset($result[$ticketCatId]['tickets'])) {
            $result[$ticketCatId]['tickets'] = array();
        }

        // this code is append the ticket ID
        if($ticketId && !isset($result[$ticketCatId]['tickets'][$ticketId])) {
            $ticket = array(
                'ticket_id'                         => $Value['ticket_id'],
                'ticket_title'                      => $Value['ticket_title'],
                'ticket_order'                      => $Value['ticket_order'],
                'due_date'                          => $Value['due_date'],
                'created_date'                      => $Value['created_date'],
                'edited_date'                       => $Value['edited_date'],
                'updated_date'                      => $Value['updated_date'],
                'is_complete'                       => $Value['is_complete'],
                'total_comment'                     => $Value['total_comment'],
                'total_attachment'                  => $Value['total_attachment'],
                'total_checklist_items'             => $Value['total_checklist_items'],
                'total_completed_checklist_items'   => $Value['total_completed_checklist_items'],
                'label' => array(),
                'people' => array()

            );
            $result[$ticketCatId]['tickets'][$ticketId] = $ticket;
        }

        if($ticketColorCode && isset($result[$ticketCatId]['tickets'][$ticketId]) && !isset($result[$ticketCatId]['tickets'][$ticketId]['label'][$ticketColorCode])) {
            $ticketColor = array(
                'ticket_label_name' => $Value['ticket_label_name'],
                'ticket_color_code' => $Value['ticket_color_code']
            );
            $result[$ticketCatId]['tickets'][$ticketId]['label'][$ticketColorCode] = $ticketColor;
        }

        if($ticketMember && isset($result[$ticketCatId]['tickets'][$ticketId]) && !isset($result[$ticketCatId]['tickets'][$ticketId]['people'][$ticketMember])) {
            $ticketPeople = array(
                'user_id' => $Value['user_id'],
                'photo' => $Value['photo']
            );
            $result[$ticketCatId]['tickets'][$ticketId]['people'][$ticketMember] = $ticketPeople;
        }
}

    dd(json_encode(["data"=>$result]));

1 个答案:

答案 0 :(得分:0)

我没有任何示例数据可以使用,因此我无法为您进行实际测试。不过,通常的想法是,每次循环时,都要初始化一个临时数组($tmpArray = array();)并将所有特定于循环的值放入该数组中。然后,在每次循环迭代结束时,将该临时数组推入$results数组($result[] = $tmpArray;)上,从而使PHP可以自动为$results创建顺序索引。当前获取嵌套JSON对象的原因是因为json_encode()仅从顺序索引的PHP数组生成JSON数组

  $result = array();
    foreach($total_count as $Key => $Value){
        $ticketCatId = $Value['ticket_category_id'];
        $ticketId = $Value['ticket_id'];
        $ticketColorCode = $Value['ticket_color_code'];
        $ticketMember = $Value['user_id'];

        $tmpArray = array();

        $tmpArray['ticket_category_id'] = $Value['ticket_category_id'];
        $tmpArray['ticket_category_name'] = $Value['ticket_category_name'];
        $tmpArray['ticket_category_order']  = $Value['ticket_category_order'];

        if(!isset($tmpArray['tickets'])) {
            $tmpArray['tickets'] = array();
        }

        // this code is append the ticket ID
        if($ticketId && !isset($tmpArray['tickets'][$ticketId])) {
            $ticket = array(
                'ticket_id'                         => $Value['ticket_id'],
                'ticket_title'                      => $Value['ticket_title'],
                'ticket_order'                      => $Value['ticket_order'],
                'due_date'                          => $Value['due_date'],
                'created_date'                      => $Value['created_date'],
                'edited_date'                       => $Value['edited_date'],
                'updated_date'                      => $Value['updated_date'],
                'is_complete'                       => $Value['is_complete'],
                'total_comment'                     => $Value['total_comment'],
                'total_attachment'                  => $Value['total_attachment'],
                'total_checklist_items'             => $Value['total_checklist_items'],
                'total_completed_checklist_items'   => $Value['total_completed_checklist_items'],
                'label' => array(),
                'people' => array()

            );
            $tmpArray['tickets'][$ticketId] = $ticket;
        }

        if($ticketColorCode && isset($tmpArray['tickets'][$ticketId]) && !isset($tmpArray['tickets'][$ticketId]['label'][$ticketColorCode])) {
            $ticketColor = array(
                'ticket_label_name' => $Value['ticket_label_name'],
                'ticket_color_code' => $Value['ticket_color_code']
            );
            $tmpArray['tickets'][$ticketId]['label'][$ticketColorCode] = $ticketColor;
        }

        if($ticketMember && isset($tmpArray['tickets'][$ticketId]) && !isset($tmpArray['tickets'][$ticketId]['people'][$ticketMember])) {
            $ticketPeople = array(
                'user_id' => $Value['user_id'],
                'photo' => $Value['photo']
            );
            $tmpArray['tickets'][$ticketId]['people'][$ticketMember] = $ticketPeople;
        }

        $result[] = $tmpArray;
}

    dd(json_encode(["data"=>$result]));