我想再将一个值id推送到内容值。
我想添加照片ID
$id=$comment_fet['ID'];`
到内容值
$content = $comment_fet['CONTENT_VALUE'];
现在
$content="{
"name": "ghggh",
"commentuser": "jghjhgjghj",
"content_type": "alb_comment",
"website_id": "571710720",
"id": 86,
"nodes": [],
"date": "2015-12-14T06:39:25.921Z",
"displayDate": "Mon Dec 14 2015 12:09:25 GMT+0530 (India Standard Time)",
"Like": 0,
"Unlike": 0,
"rating": 0,
"reportAbuse": 0
}"
function get_album_comment($prefix) {
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
// print_r($request);
$id = $request->photoid;
$sql = "select * from user_comment where SUB_ID='$id'";
$query = mysql_query($sql) or sqlerrorhandler("(" . mysql_errno() . ") " . mysql_error(), $sql, __LINE__);
//$datas = array();
while ($comment_fet = mysql_fetch_assoc($query)) {
$content = $comment_fet['CONTENT_VALUE'];
$id=$comment_fet['ID'];
$datas[] = json_decode($content);
}
echo $get_like = json_encode($datas);
}
答案 0 :(得分:0)
试试这个:
<?php
$temp=json_decode($content); // decodes it to json
$temp->id=$comment_fet['ID']; // appends photo id to it
$content=json_encode($temp); // encodes it back
?>
答案 1 :(得分:0)
如果你想将一个新值(比如photo_id
)推送到json字符串,那么你可以这样做:
// your code
$comment_fet = mysql_fetch_assoc($query);
$content = $comment_fet['CONTENT_VALUE'];
$id=$comment_fet['ID'];
$datas = json_decode($content, true);
$datas['photo_id'] = $id;
$content=json_encode($datas);
// your code
旁注:请不要使用mysql_
数据库扩展,它们在PHP 5.5.0中已弃用,并已在PHP 7.0.0中删除。请改用mysqli
或PDO
扩展程序。这是why you shouldn't use mysql_
functions。
<强>编辑:强>
// your code
$contents = array(); // $contents array will contain all contents
while($comment_fet = mysql_fetch_assoc($query)){
$content = $comment_fet['CONTENT_VALUE'];
$id=$comment_fet['ID'];
$datas = json_decode($content, true);
$datas['photo_id'] = $id;
$contents[] = json_encode($datas);
}
// loop through the $contents array to display all contents
for($i = 0; $i < count($contents); ++$i){
echo $contents[$i] . "<br />";
}
// your code