0

我有一个问题,
我从数组数据创建了多个 json,但是我有问题输出,json 子菜单或子菜单总是打印每个元素,该 json 子菜单应该打印在字段 media_menu_parent_id 的关系中

你能帮帮我吗?

我可以发布我的图片表,因为我的声誉 < 10 :(

这是我的代码控制器

header("Content-type: application/json; charset=utf-8");
    $main_arr = array();

    if (!$mediaId) {
        $main_arr['status'] = "nok";
        $main_arr['message'] = "Media id is null";
        $json = json_encode($main_arr);
        echo $json;
        exit();
    }

    $where = array( 'media_menu.status'=>1 ,
                    'media_menu.media_menu_parent_id'=>0,
                    'media_menu.media_id'=>$mediaId
                  );

    $count = $this->mediamenumodel->getCountMediaMenu($where);
    $result = $this->mediamenumodel->getMediaMenu($where,$offest,$page);

    if (!$result) {
        $main_arr['status'] = "nok";
        $main_arr['message'] = "Data not Found";
        $json = json_encode($main_arr);
        echo $json;
        exit();
    }

    $main_arr['rowCount'] = $count;
    $main_arr['media_id'] = $result[0]['media_id'];
    $main_arr['media_name'] = $result[0]['media_name'];

    $h=0;
    foreach($result as $key => $value)
    {    
        $where1 = array('media_menu_parent_id' => $value['media_menu_id']);
        $count1 = $this->mediamenumodel->getCountMediaMenu($where1);
        $getMediaMenu1 = $this->mediamenumodel->getMediaMenu($where1, $count1);

        $arr['media_menu_id']   = $value['media_menu_id'];
        $arr['media_menu_name'] = $value['media_menu_name'];
        $arr['media_menu_url']  = $value['media_menu_url'];
        $arr['thumbnail']       = !empty($value['thumbnail']) ? URL_CONTENT_MENU_THUMB.$value['thumbnail'] : "";

        if ($count1>0) {
            $i=0;
            $arr['lewat'] = "ok";
            foreach ($getMediaMenu1 as $key1 => $value1){
                $arr['submenu'][$i]['media_menu_id']    = $value1['media_menu_id'];
                $arr['submenu'][$i]['media_menu_name']  = $value1['media_menu_name'];
                $arr['submenu'][$i]['media_menu_url']   = $value1['media_menu_url'];
                $arr['submenu'][$i]['thumbnail']        = !empty($value1['thumbnail']) ? URL_CONTENT_MENU_THUMB.$value['thumbnail'] : "";


                $i++;

            }

        }

        $main_arrx[] = $arr;
        $h++;
    }

    $main_arr['data'] = $main_arrx;

    $json = json_encode($main_arr);
    echo $json;

可以确定我的模型运行良好,我尝试修改数组格式,但没有成功

这是json输出:

{

"rowCount": 5,
"media_id": "1",
"media_name": "sites",
"data": [
    {
        "media_menu_id": "5",
        "media_menu_name": "Games",
        "media_menu_url": "",
        "thumbnail": "",
        "submenu": [
            {
                "media_menu_id": "7",
                "media_menu_name": "Games 2",
                "media_menu_url": "",
                "thumbnail": "",
                "submenu": [
                    {
                        "media_menu_id": "9",
                        "media_menu_name": "Games 2-2",
                        "media_menu_url": null,
                        "thumbnail": ""
                    },
                    {
                        "media_menu_id": "8",
                        "media_menu_name": "Games 2-1",
                        "media_menu_url": null,
                        "thumbnail": ""
                    }
                ]
            },
            {
                "media_menu_id": "6",
                "media_menu_name": "Games 1",
                "media_menu_url": null,
                "thumbnail": ""
            }
        ]
    },
    {
        "media_menu_id": "4",
        "media_menu_name": "Application",
        "media_menu_url": null,
        "thumbnail": "",
        "submenu": [
            {
                "media_menu_id": "7",
                "media_menu_name": "Games 2",
                "media_menu_url": "",
                "thumbnail": "",
                "submenu": [
                    {
                        "media_menu_id": "9",
                        "media_menu_name": "Games 2-2",
                        "media_menu_url": null,
                        "thumbnail": ""
                    },
                    {
                        "media_menu_id": "8",
                        "media_menu_name": "Games 2-1",
                        "media_menu_url": null,
                        "thumbnail": ""
                    }
                ]
            },
            {
                "media_menu_id": "6",
                "media_menu_name": "Games 1",
                "media_menu_url": null,
                "thumbnail": ""
            }
        ]
    },
    {
        "media_menu_id": "3",
        "media_menu_name": "Rich Content",
        "media_menu_url": null,
        "thumbnail": "",
        "submenu": [
            {
                "media_menu_id": "7",
                "media_menu_name": "Games 2",
                "media_menu_url": "",
                "thumbnail": "",
                "submenu": [
                    {
                        "media_menu_id": "9",
                        "media_menu_name": "Games 2-2",
                        "media_menu_url": null,
                        "thumbnail": ""
                    },
                    {
                        "media_menu_id": "8",
                        "media_menu_name": "Games 2-1",
                        "media_menu_url": null,
                        "thumbnail": ""
                    }
                ]
            },
            {
                "media_menu_id": "6",
                "media_menu_name": "Games 1",
                "media_menu_url": null,
                "thumbnail": ""
            }
        ]
    },
    {
        "media_menu_id": "2",
        "media_menu_name": "Top Download",
        "media_menu_url": null,
        "thumbnail": "",
        "submenu": [
            {
                "media_menu_id": "7",
                "media_menu_name": "Games 2",
                "media_menu_url": "",
                "thumbnail": "",
                "submenu": [
                    {
                        "media_menu_id": "9",
                        "media_menu_name": "Games 2-2",
                        "media_menu_url": null,
                        "thumbnail": ""
                    },
                    {
                        "media_menu_id": "8",
                        "media_menu_name": "Games 2-1",
                        "media_menu_url": null,
                        "thumbnail": ""
                    }
                ]
            },
            {
                "media_menu_id": "6",
                "media_menu_name": "Games 1",
                "media_menu_url": null,
                "thumbnail": ""
            }
        ]
    },
    {
        "media_menu_id": "1",
        "media_menu_name": "Hot Promo",
        "media_menu_url": "",
        "thumbnail": "",
        "submenu": [
            {
                "media_menu_id": "10",
                "media_menu_name": "Hot Promo 1",
                "media_menu_url": null,
                "thumbnail": "",
                "submenu": [
                    {
                        "media_menu_id": "9",
                        "media_menu_name": "Games 2-2",
                        "media_menu_url": null,
                        "thumbnail": ""
                    },
                    {
                        "media_menu_id": "8",
                        "media_menu_name": "Games 2-1",
                        "media_menu_url": null,
                        "thumbnail": ""
                    }
                ]
            },
            {
                "media_menu_id": "6",
                "media_menu_name": "Games 1",
                "media_menu_url": null,
                "thumbnail": ""
            }
        ]
    }
]

}

我想要这样的输出:

{

"rowCount": 5,
"media_id": "1",
"media_name": "sites",
"data": [
    {
        "media_menu_id": "5",
        "media_menu_name": "Games",
        "media_menu_url": "",
        "thumbnail": "",
        "submenu": [
            {
                "media_menu_id": "7",
                "media_menu_name": "Games 2",
                "media_menu_url": "",
                "thumbnail": "",
                "submenu": [
                    {
                        "media_menu_id": "9",
                        "media_menu_name": "Games 2-2",
                        "media_menu_url": null,
                        "thumbnail": ""
                    },
                    {
                        "media_menu_id": "8",
                        "media_menu_name": "Games 2-1",
                        "media_menu_url": null,
                        "thumbnail": ""
                    }
                ]
            },
            {
                "media_menu_id": "6",
                "media_menu_name": "Games 1",
                "media_menu_url": null,
                "thumbnail": ""
            }
        ]
    },
    {
        "media_menu_id": "4",
        "media_menu_name": "Application",
        "media_menu_url": null,
        "thumbnail": ""
    },
    {
        "media_menu_id": "3",
        "media_menu_name": "Rich Content",
        "media_menu_url": null,
        "thumbnail": ""
    },
    {
        "media_menu_id": "2",
        "media_menu_name": "Top Download",
        "media_menu_url": null,
        "thumbnail": ""
    },
    {
        "media_menu_id": "1",
        "media_menu_name": "Hot Promo",
        "media_menu_url": "",
        "thumbnail": "",
        "submenu": [
            {
                "media_menu_id": "10",
                "media_menu_name": "Hot Promo 1",
                "media_menu_url": null,
                "thumbnail": ""
            }
        ]
    }
]

}

谢谢,

4

1 回答 1

0

请试试这个:

foreach($result as $key => $value){ 
    $where1 = array('media_menu_parent_id' => $value['media_menu_id']);
    $count1 = $this->mediamenumodel->getCountMediaMenu($where1);
    $getMediaMenu1 = $this->mediamenumodel->getMediaMenu($where1, $count1);
    $arr = []; // reset before constructing a new parent..
于 2013-03-25T10:31:44.753 回答