简体   繁体   English

将值推入数组

[英]pushing a value to array

I want to push one more value id to the content value. 我想将另一个值id推送到内容值。

I want to add Photo id 我想添加带照片的身份证

$id=$comment_fet['ID'];`

to content value 内容价值

$content = $comment_fet['CONTENT_VALUE'];

Now 现在

$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);
}

Try this: 尝试这个:

<?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
?>

If you want to push a new value(say photo_id ) to the json string, then you can do something like this: 如果要将新值(例如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

Sidenote: Please don't use the mysql_ database extensions, they were deprecated in PHP 5.5.0 and were removed in PHP 7.0.0. 旁注:请不要使用mysql_数据库扩展名,它们在PHP 5.5.0中已弃用,在PHP 7.0.0中已删除。 Use mysqli or PDO extensions instead. 改用mysqliPDO扩展。 And this is why you shouldn't use mysql_ functions . 这就是为什么您不应该使用mysql_函数

Edited: 编辑:

// 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM