用json在json中添加一个json项

时间:2013-10-04 19:22:41

标签: php json angularjs

我正在尝试使用php后端创建一个角度应用程序,现在我遇到了问题。我有一个PHP代码,我将它粘贴在这里。

<?php
    $textdata = '{"contact_firstname":"Daniel","contact_lastname":"Pacuraru","contact_email":["pacurarudaniel@gmail.com","hello@pacurarudaniel.com"]}';
    $json = json_decode($textdata, true);
    //$json = json_decode(file_get_contents('php://input'), true);

    $dbhost = "localhost";
    $dbport = "5432";
    $dbname = "fixevents";
    $dbuser = "postgres";
    $dbpass = "123";
    $connect = pg_connect("host=" . $dbhost . " port=" . $dbport . " dbname=" . $dbname . " user=" . $dbuser . " password=" . $dbpass);

    $query = "INSERT INTO contact (contact_firstname, contact_lastname, contact_email) VALUES ('" . addslashes($json['contact_firstname']) . "', '" . addslashes($json['contact_lastname']) . "', '" . addslashes(implode(',,,', $json['contact_email'])) . "') RETURNING contact_id";
    $result = pg_query($connect, $query);

    $row = pg_fetch_array($result);
    $json_item = 'contact_id' => addslashes($row['contact_id']);
    array_push($json, $json_item);

    echo json_encode($json);
?>

现在我添加了一个静态json作为输入数据。 我看到angular在http get和post之后返​​回一个值,所以我想要实现的是返回带有添加到数据库中的ID的json。我用SQL返回contact_id来重新启动这个id,我想将它插入到json中,但我不知道如何。 将该项插入json之后,我希望它看起来像这样

{"contact_id":"1","contact_firstname":"Daniel","contact_lastname":"Pacuraru","contact_email":["pacurarudaniel@gmail.com","hello@pacurarudaniel.com"]}

实际上将"contact_id":"1"添加到json中。谢谢你,丹尼尔。

2 个答案:

答案 0 :(得分:1)

它对我有用。

<?php

    $textdata = '{"contact_firstname":"Daniel","contact_lastname":"Pacuraru","contact_email":["pacurarudaniel@gmail.com","hello@pacurarudaniel.com"]}';
    /*
    Finding contact_id ...
    $contact_id = 1; // example

    */
    $contact_id = 1;
    $json = json_decode($textdata,true);
    $json = array('contact_id'=>addslashes($contact_id)) + $json; 
    echo json_encode($json);

结果:

{"contact_id":"1","contact_firstname":"Daniel","contact_lastname":"Pacuraru","contact_email":["pacurarudaniel@gmail.com","hello@pacurarudaniel.com"]}

根据您的代码:

<?php
    $textdata = '{"contact_firstname":"Daniel","contact_lastname":"Pacuraru","contact_email":["pacurarudaniel@gmail.com","hello@pacurarudaniel.com"]}';
    $json = json_decode($textdata, true);
    //$json = json_decode(file_get_contents('php://input'), true);

    $dbhost = "localhost";
    $dbport = "5432";
    $dbname = "fixevents";
    $dbuser = "postgres";
    $dbpass = "123";
    $connect = pg_connect("host=" . $dbhost . " port=" . $dbport . " dbname=" . $dbname . " user=" . $dbuser . " password=" . $dbpass);

    $query = "INSERT INTO contact (contact_firstname, contact_lastname, contact_email) VALUES ('" . addslashes($json['contact_firstname']) . "', '" . addslashes($json['contact_lastname']) . "', '" . addslashes(implode(',,,', $json['contact_email'])) . "') RETURNING contact_id";
    $result = pg_query($connect, $query);

    $row = pg_fetch_array($result);
    $json_item = array('contact_id' => addslashes($row['contact_id']));
    $json = $json_item + $json;
    echo json_encode($json);
?>

答案 1 :(得分:0)

删除行:

$json_item = 'contact_id' => addslashes($row['contact_id']);
array_push($json, $json_item);

添加行:

$json_item = array('contact_id' => addslashes($row['contact_id']));
$json = $json_item + $json;

它会将contact_id获取到第一个索引。但是如果你想要它在最后一个索引上然后忽略上面的行并执行:

$json['contact_id'] = addslashes($row['contact_id'])