如何使用php创建如下所示的json?

时间:2019-01-02 08:27:41

标签: php arrays json rest

我正在使用PHP Codeigniter编写Angular应用程序的API。 我无法为我的应用程序创建json响应。

请找到参考网址https://ionicacademy.com/accordion-list-ionic/

我正在尝试使用以上示例创建多级手风琴。

这就是我从代码中得到的

{
    "items":[
        {
            "name": "VAL 1",
            "children": [
                {
                    "name": "Topic 1"
                },
                {
                    "name": {
                    "children": "Children 2"
                    }
                },
                {
                    "name": "Topic 3"
                },
                {
                    "name": {
                    "children": "Children 2"
                    }
                }
            ]
        }
    ]
}

找到以下代码以供参考。

$sec_pair = array('1' => 'VAL 1', '2' => 'VAL 2', '3' => 'VAL 3');
$data['items'] = array();
if(!empty($videos)) {
    foreach ($videos AS $key => $item) {
        $title_str = !empty($sec_pair[$key]) ? $sec_pair[$key] : '';
        $topics = array();
        $topics['name'] = $title_str;
        foreach ($item AS $k => $i_val) {
            $lectures_title = $this->my_model->select_db('topics', 'id,title', array('id' => $k));
            $topic_title = !empty($lectures_title[0]->title) ? $lectures_title[0]->title : '';
            $topics['children'][]['name'] = $topic_title;
            $topics['children'][]['name']['children'] = 'Children 2';
        }
        $data['items'][] = $topics;
    }
}
header('Content-Type: application/json');
echo json_encode($data, TRUE);
exit;

这就是我想要的

{
"items": [
    {
        "name": "Action",
        "children": [
            {
                "name": "Special Academy Pizza",
                "information": "Pastrami pork belly ball tip andouille corned beef jerky shankle landjaeger. Chicken chuck porchetta picanha, ham brisket tenderloin venison meatloaf landjaeger jowl.",
                "price": "$25"
            },
            {
                "name": "Pizza Ionic",
                "information": "Pork chop meatloaf rump, meatball shoulder turducken alcatra doner sausage capicola pork strip steak turkey cupim leberkas.",
                "price": "$19.99"
            }
        ]
    },
    {
        "name": "Pizza",
        "children": [
            {
                "name": "Traditional",
                "children": [
                    {
                        "name": "Pizza Salami",
                        "information": "Pork chop jowl capicola porchetta, kielbasa prosciutto boudin bacon pork pig.",
                        "price": "$10"
                    },
                    {
                        "name": "Pizza Prosciutto",
                        "information": "Pork chop pastrami landjaeger chuck brisket",
                        "price": "$12"
                    }
                ]
            },
            {
                "name": "Gourmet",
                "children": [
                    {
                        "name": "Pizza Bombay",
                        "information": "Pastrami ham hock ball tip, tongue ribeye chuck ham beef bresaola leberkas.",
                        "price": "$13"
                    },
                    {
                        "name": "Pizza Crazy Dog",
                        "information": "Andouille spare ribs meatloaf swine ground round pork loin, brisket chuck bacon tongue.",
                        "price": "$14"
                    },
                    {
                        "name": "Pizza Italia",
                        "information": "Ribeye ham t-bone, tail ground round biltong picanha sausage rump corned beef.",
                        "price": "$11"
                    },
                    {
                        "name": "Pizza Tuna",
                        "information": "Pork chop pastrami landjaeger chuck brisket",
                        "price": "$14"
                    }
                ]
            },
            {
                "name": "Bestseller",
                "children": [
                    {
                        "name": "Pizza Academy",
                        "information": "Frankfurter tail capicola cupim shankle salami, beef ribs beef boudin porchetta ball tip leberkas turkey tenderloin.",
                        "price": "$25"
                    },
                    {
                        "name": "Pizza Ionic",
                        "information": "Shank chuck tail, kevin shankle ham hock pork loin pork hamburger beef ribs.",
                        "price": "$19.99"
                    }
                ]
            }
        ]
    },
    {
        "name": "Drinks",
        "children": [
            {
                "name": "Special Academy Pizza",
                "information": " Landjaeger fatback shank frankfurter, tongue shoulder ham strip steak pancetta pork short loin corned beef short ribs biltong cow",
                "price": "$25"
            },
            {
                "name": "Pizza Ionic",
                "information": "Pork chop pastrami landjaeger chuck brisket",
                "price": "$19.99"
            }
        ]
    }
]

}

0 个答案:

没有答案