使用数据库在PHP中创建Json [英] Create Json in PHP using database

查看:107
本文介绍了使用数据库在PHP中创建Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Everyone,
我有这个查询,我运行从三个表收集数据。我想从这个收集的数据创建Json。我写了以下脚本来创建它。如果有任何快速或聪明的解决方法,请指导我。



这是我需要创建JSON的数据库结果的快照。



我使用的脚本是这个。

  $ jsonarray = array 

while($ i {
$ channel_cat = Array(Channel Category=> Array(name=> $ result [$ i] ['chanct_name']));

$ id = $ result [$ i] ['chanct_id'];
while($ id == $ result [$ i] ['chanct_id'])
{
$ content [] = Array(Channel Name=> $ result [$ i ] ['con_name'],image=> $ result [$ i] ['con_image']);
$ i ++;
}

$ jsonarray [] [] = Array($ channel_cat,$ content);
unset($ content);

}
echo json_encode($ jsonarray);

这给我下面粘贴的结果。

  [{Channel Name:Channel1,image: 频道名称:频道名称:频道名称:Channel2,image: amazon-logo-b_tpng.png},
{Channel Name:High,image:Thanks.jpg}]]]
,[[{Channel Category {name:Documentary}},
[{Channel Name:Channel7,image:amazon-logo-b_tpng.png}]]] code>



但我正在查找以下结果。

 code> {
channelscategories:
[
{
name:Movie,
image:MoviePoster,
contents:
[
{
name:Channel 1,
image:Thanks.jpg,
},
{
name:Channel 2,
image:Thanks.jpg,
},
{
name: Channel 4,
image:amazon-logo ...,
},
{
name:High,
image:Thanks.jpg,
}
]

},
{
name:纪录片,
image:MoviePoster,
contents:
[
{
name:Channel 7,
image:amazon。 ....,
}
]

}


]
}

任何帮助或指导将是非常有帮助的。感谢

解决方案

尝试此操作,它会给您预期的结果:

  $ typeArr = array(); 
foreach($ result as $ a){
$ typeArr [$ a ['chanct_name']] [] = array(
'name'=> $ a ['con_name'] ,
'image'=> $ a ['con_image']
);
}

$ jsonarray = array();
foreach($ typeArr as $ type => $ contents){
$ jsonarray ['channelsCategories'] [] = array(
'name'=> $ type,
'contents'=> $ contents
);
}

echo json_encode($ jsonarray);


Hello Everyone, I have this query that I have run to gather data from three tables. I want to create Json from this data gathered. I have written the following script to create it. Please guide me if there is any quick or smart workaround to do this.

This is the snapshot of my database result that I need to create JSON from.

Script that I'm using is this.

    $jsonarray = array();

    while ($i<count($result))
   {
        $channel_cat = Array("Channel Category" => Array("name" => $result[$i]['chanct_name']));

        $id = $result[$i]['chanct_id'];
    while($id == $result[$i]['chanct_id'])
        {
            $content[] = Array( "Channel Name" => $result[$i]['con_name'], "image" => $result[$i]['con_image']); 
            $i++;
        }

        $jsonarray[][] = Array($channel_cat, $content);
        unset($content);

   }
     echo json_encode($jsonarray);

This gives me result pasted below.

[[[{"Channel Category":{"name":"Movie"}},
[{"Channel Name":"Channel1","image":"Thanks.jpg"},
{"Channel Name":"Channel2","image":"Thanks.jpg"},
{"Channel Name":"Channel4","image":"amazon-logo-b_tpng.png"},
{"Channel Name":"High","image":"Thanks.jpg"}]]]
,[[{"Channel Category":{"name":"Documentary"}},
[{"Channel Name":"Channel7","image":"amazon-logo-b_tpng.png"}]]]]

However I am looking for result below.

{
"channelsCategories":
    [
        {
            "name":"Movie",
            "image": "MoviePoster",
            "contents":
                [
                    {
                        "name":"Channel 1",
                        "image":"Thanks.jpg",
                    },
                    {
                        "name":"Channel 2",
                        "image":"Thanks.jpg",
                    },
                    {
                        "name":"Channel 4",
                        "image":"amazon-logo...",
                    },
                    {
                        "name":"High",
                        "image":"Thanks.jpg",
                    }
            ]

        },
        {
            "name":"Documentary",
            "image": "MoviePoster",
            "contents":
                [
                    {
                        "name":"Channel 7",
                        "image":"amazon.....",
                    }
            ]

        }


    ]
}

Any help or guidance will be really helpful. Thanks

解决方案

Try this, it will give you the result you are expecting:

$typeArr = array();
foreach($result as $a){
    $typeArr[$a['chanct_name']][] = array(
        'name'=>$a['con_name'],
        'image'=>$a['con_image']
    );
}

$jsonarray = array();
foreach($typeArr as $type=>$contents){
    $jsonarray['channelsCategories'][] = array(
        'name'=>$type,
        'contents'=>$contents
    );
}

echo json_encode($jsonarray);

这篇关于使用数据库在PHP中创建Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆