Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.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
Php 在JSON中添加其他对象_Php_Mysql_Arrays_Json - Fatal编程技术网

Php 在JSON中添加其他对象

Php 在JSON中添加其他对象,php,mysql,arrays,json,Php,Mysql,Arrays,Json,我目前正在使用一个JSON编码的数组在我的数据库中显示一个自动建议功能的标题 它看起来像这样: <?php require_once('./includes/config.php'); require_once('./includes/skins.php'); mysql_connect($conf['host'], $conf['user'], $conf['pass']); mysql_select_db($conf['name']); $query2012 = sprin

我目前正在使用一个JSON编码的数组在我的数据库中显示一个自动建议功能的标题

它看起来像这样:

<?php
require_once('./includes/config.php');
require_once('./includes/skins.php');

mysql_connect($conf['host'], $conf['user'], $conf['pass']);
mysql_select_db($conf['name']);

    $query2012 = sprintf("SELECT * FROM imdb WHERE poster !='posters/noposter.jpg' ORDER BY RAND() DESC LIMIT %d;", 8);
    $result = mysql_query($query2012);



    //Create an array
    $json_response = array();

    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $row_array['title'] = $row['title'];
        $row_array['year'] = $row['year'];
        $row_array['poster'] = $row['poster'];

        //push the values in the array
        array_push($json_response,$row_array);


    }

    echo json_encode($json_response);

    //Close the database connection
    fclose($db);

?>
首先,如何手动向该输出中添加其他对象?例如,假设我想添加:
{“状态”:“确定”,“消息”:“成功”,“数据”:

如果未找到mysql记录,则显示json输出

{"status":"error","message":"No Reord found"}

如何添加此项?

您可以在编码(
json\u encode()
之前将其添加到$json\u响应数组中,方法是修改它的结构:

$json_response = array(
    'data' => $json_response,
    'status' => 'ok',
    'message' => 'Successs'
);
您还可以修改将数据附加到最终结果变量,以便在定义$json_响应数组时添加子数组:

$json_response = array('data' => array());
并在while循环中添加索引数据:

循环后,您可以通过以下方式轻松附加状态和消息:

$json_response['status'] = 'ok';
$json_response['message'] = 'Success';

要添加错误,只需检查数据数组是否为空。对于第一个解决方案:

if (empty($json_response)) {
    $json_response = array(
        'status' => 'error',
        'message' => 'No Reord found'
    );
} else {
    //  here append success message
}
在第二种情况下,只需将if条件更改为:

if (empty($json_response['data']))
$json_response['status'] = 'ok';
$json_response['message'] = 'Success';
if (empty($json_response)) {
    $json_response = array(
        'status' => 'error',
        'message' => 'No Reord found'
    );
} else {
    //  here append success message
}
if (empty($json_response['data']))