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 に答える