Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用数据库II在PHP中创建Json_Php_Json - Fatal编程技术网

使用数据库II在PHP中创建Json

使用数据库II在PHP中创建Json,php,json,Php,Json,我以前问过这个问题 这一点得到了回答,它也起了作用,但需求发生了一些变化,我试图获得的新输出并没有通过这个脚本实现 这个脚本给了我这个输出 { "channelsCategories": [ { "name":"Movie", "contents": [ { "name":"Channel 1",

我以前问过这个问题

这一点得到了回答,它也起了作用,但需求发生了一些变化,我试图获得的新输出并没有通过这个脚本实现

这个脚本给了我这个输出

{
"channelsCategories":
    [
        {
            "name":"Movie",
            "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",
            "contents":
                [
                    {
                        "name":"Channel 7",
                        "image":"amazon.....",
                    }
            ]

        }


    ]
}
新的要求是在类别数组中以及下面添加一个图像字段

{
"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.....",
                    }
            ]

        }


    ]
}
到目前为止,我已经实现了这个数组结果,我希望通过它循环并获得上面的输出

array(8) { 
    ["Movieposter"]=> string(7) "Movieposter" 
    ["News"]=> array(2) { 
        [0]=> array(5) { 
            ["name"]=> string(8) "Dunya tv" 
            ["url"]=> string(4) "http" 
            ["image"]=> string(4) "http" 
            ["type"]=> string(1) "3" 
            ["status"]=> string(6) "Enable" 
        } 
        [1]=> array(5) { 
            ["name"]=> string(6) "Geo tv" 
            ["url"]=> string(4) "http" 
            ["image"]=> string(4) "http" 
            ["type"]=> string(1) "3" 
            ["status"]=> string(6) "Enable" 
        } 
    } 
    ["Dramaimg"]=> string(8) "Dramaimg" 
    ["Drama"]=> array(2) { 
        [0]=> array(5) { 
            ["name"]=> string(7) "drama 1" 
            ["url"]=> string(4) "http" 
            ["image"]=> string(4) "http" 
            ["type"]=> string(1) "3" 
            ["status"]=> string(6) "Enable" 
        } 
        [1]=> array(5) { 
            ["name"]=> string(7) "drama 2" 
            ["url"]=> string(4) "http" 
            ["image"]=> string(4) "http" 
            ["type"]=> string(1) "4" 
            ["status"]=> string(6) "Enable" 
        } 
    } 
    ["entertainmentimg"]=> string(16) "entertainmentimg" 
    ["Entertainment"]=> array(1) { 
        [0]=> array(5) { 
            ["name"]=> string(8) "indian 1" 
            ["url"]=> string(4) "http" 
            ["image"]=> string(4) "http" 
            ["type"]=> string(1) "3" 
            ["status"]=> string(6) "Enable" 
        } 
    } 
    ["Pakistanimg"]=> string(11) "Pakistanimg" 
    ["Pakistan"]=> array(1) { 
        [0]=> array(5) { 
            ["name"]=> string(8) "pcontent" 
            ["url"]=> string(8) "pcontent" 
            ["image"]=> string(4) "http" 
            ["type"]=> string(1) "7" 
            ["status"]=> string(6) "Enable" 
        } 
    } 
}

任何帮助和指导都将不胜感激。

以下是为我完成工作的脚本

$typeArr = array();
foreach($result as $a){
    $typeArr[$a['chanct_image']] = $a['chanct_image'];
    $typeArr[$a['chanct_name']][] = array(
        'name'=>$a['con_name'],
        'url'=>$a['con_url'],
        'image'=>$a['con_image'],
        'type'=>$a['con_type'],
        'status'=>$a['con_status']
    );
}

$jsonarray = array();
foreach($typeArr as $type=>$contents){

    if(is_array($contents) == FALSE){
    $image = $type;
    }
    if(is_array($contents))
    {               
        $jsonarray['channelsCategories'][] = array(
            'name'=>$type,
            'image'=>$image,
            'contents'=>$contents
        );
    }
}

在第一个循环中添加image字段,然后在第二个循环中检查数组值是array还是string,并基于填充第二个jsonarray的值进行检查。

您尝试了什么?如果您理解了上一个问题的基本原理,那么修改似乎应该是显而易见的。只需使用该问题中的解决方案,并将
'image'=>'MoviePoster'
添加到第二个
foreach
循环中的数组中。我已经尝试过@Barmar,但它会,而且不起作用,因为第二个循环中的数组只有名称和内容,它正在循环。在循环一中,我们只设置了两个东西名称和内容。IMO的更改应该先在第一个循环中进行,然后在第二个循环中进行,但我无法确定应该进行什么更改……您能否以可读的方式显示数组格式,而不是全部显示在一行上?请使用
if/else
而不是两个只测试相反条件的
if
语句。Thnx@Barmar提供所有帮助。