json_en code变为数组对象 [英] json_encode turns array into an object

查看:176
本文介绍了json_en code变为数组对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了href=\"http://www.jamesthistlewood.co.uk/chatter/index.php\" rel=\"nofollow\">社交媒体网站我正在一个一个但我有一点麻烦json_en code和json_de code。我用数组存储谁已经喜欢上了一个帖子,所以我需要这两种功能,使其可存储,但由于某种原因它有时会保存对象到数据库:

I am have created a 'liking' system for a social media website I'm working on but I'm having a bit of trouble with json_encode and json_decode. I use arrays to store who has liked a post, so I need these 2 functions to make it storable, but for some reason it sometimes saves an object to the database:

{1:管理}

而不是数组(我想):

[管理员]

这是我的code:

if ($liked_by == NULL){ $liked_by = Array(); }
if (! in_array($user, $liked_by)){
  $liked_by[] = $user;
  $likes = $row['likes']+1;
  $liked_by = json_encode($liked_by);
  mysql_query("UPDATE $stream SET liked_by = '$liked_by' WHERE id = ".$id, $db)
    or die(mysql_error($db));
  mysql_query("UPDATE $stream SET likes = '$likes' WHERE id = ".$id, $db)
    or die(mysql_error($db));
} else{
  if(($liker = array_search($user, $liked_by)) !== false) {
    unset($liked_by[$liker]);
  }
  $likes = $row['likes']-1;
  $liked_by = json_encode($liked_by);
  mysql_query("UPDATE $stream SET liked_by = '$liked_by' WHERE id = ".$id, $db)
    or die(mysql_error($db));
  mysql_query("UPDATE $stream SET likes = '$likes' WHERE id = ".$id, $db)
    or die(mysql_error($db));
}

对不起使用去precated MySQL的功能...

Sorry about using deprecated mysql functions...

我只是不知道发生了什么。
先谢谢了。

I'm just not sure what's happening. Thanks in advance.

推荐答案

您可以为

json_decode($data,true);

其中, $数据是从数据库中保存的数据。

where $data is the saved data from DB.

以上将返回一个数组,你可以操纵它进行进一步的操作。

The above will return an array and you can manipulate it for further operation.

这篇关于json_en code变为数组对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆