Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.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
如何使用JSON(PHP)获取不同的列?_Php_Json - Fatal编程技术网

如何使用JSON(PHP)获取不同的列?

如何使用JSON(PHP)获取不同的列?,php,json,Php,Json,我对PHP非常陌生,甚至不懂JSON。而且,我不知道如何在问题中表达它。我的问题是, 我需要用PHP和数据库中的数据制作一个JSON数据。因此,我的预期输出是什么样的 { "canFireAPICalls": true, "ads": { "servers": [ { "type": "OpenX", "apiAddress": "http://openx.openvideoads.org/openx/www/delivery/fc.php", } ], "schedule": [ { "zon

我对PHP非常陌生,甚至不懂JSON。而且,我不知道如何在问题中表达它。我的问题是,

我需要用PHP和数据库中的数据制作一个JSON数据。因此,我的预期输出是什么样的

{ "canFireAPICalls": true, "ads": { "servers": [ { "type": "OpenX", "apiAddress": "http://openx.openvideoads.org/openx/www/delivery/fc.php", } ], "schedule": [ { "zone": "5", "position": "pre-roll" }, { "zone": "33", "width": 450, "height": 50, "startTime": "00:00:05", "duration": "15" } ], } }
我用PHP写的是

while ($doZones->fetch() && $row = $doZones->toArray()) {
    $row_array['zone'] = $row['zoneid'];
    $row_array['position'] = $row['delivery'];

    if($row['delivery'] == 0 || $row['delivery'] == 1 || $row['delivery'] == 4 ){
        $row_array['width'] = $row['width'];
        $row_array['height'] = $row['height'];
    }


    array_push($return_arr,$row_array);
}

$resultJSON .=json_encode($return_arr);
我的输出是

{ "canFireAPICalls": true, "ads": { "servers": [ { "type": "OpenX", "apiAddress": "http://openx.openvideoads.org/openx/www/delivery/fc.php", } ], "schedule":[{"zone":"3","position":"6"},{"zone":"2","position":"6"},{"zone":"4","position":"6"},{"zone":"5","position":"6"},{"zone":"6","position":"1","width":"468","height":"60"},{"zone":"7","position":"6","width":"468","height":"60"},{"zone":"8","position":"0","width":"120","height":"600"},{"zone":"9","position":"7","width":"120","height":"600"},{"zone":"10","position":"0","width":"120","height":"600"},{"zone":"11","position":"0","width":"728","height":"90"},{"zone":"12","position":"0","width":"120","height":"90"},{"zone":"13","position":"1","width":"468","height":"60"},{"zone":"14","position":"3","width":"468","height":"60"},{"zone":"15","position":"1","width":"560","height":"40"},{"zone":"16","position":"4","width":"468","height":"60"},{"zone":"17","position":"6","width":"468","height":"60"},{"zone":"18","position":"6","width":"468","height":"60"},{"zone":"19","position":"7","width":"468","height":"60"}] } }

如果你注意到,即使没有宽度和高度,也有。我只想要$row['delivery']为0,1或4时的宽度和高度。但是,一旦找到值0、1或4,即使下一行不是0、1或4,它也会打印宽度和高度。有办法解决这个问题吗?我不知道任何JSON,只是从PHP开始。现在,我不关心缺少的标签,比如持续时间或写预卷。我将在修复此问题后处理它们。

问题出在您的php中。您不会重置数组的值,因此第一次该条件为真时,高度和宽度的值将保留在数组中

您可以通过以下方式进行修复:

while ($doZones->fetch() && $row = $doZones->toArray()) {
    $row_array['zone'] = $row['zoneid'];
    $row_array['position'] = $row['delivery'];

    if($row['delivery'] == 0 || $row['delivery'] == 1 || $row['delivery'] == 4 ){
        $row_array['width'] = $row['width'];
        $row_array['height'] = $row['height'];
    }


    array_push($return_arr,$row_array);
    // Add this line to reset the array and reset width / height
    $row_array = array();
}

$resultJSON .=json_encode($return_arr);

这与JSON无关,请确保将正确的数据输入到
$return\u arr
;i、 e使用
print\r($return\u arr)
并查看其中的内容您可以在内置php函数中使用
json\u encode($array)
json\u decode($value)
非常感谢,我不知道我应该这么做。我会在50秒内接受你的回答