【问题标题】:json_encode error Object of class stdClass could not be converted to stringjson_encode错误stdClass类的对象无法转换为字符串
【发布时间】:2013-10-01 22:06:16
【问题描述】:

我有一个 json_encode 元素,我必须像这样内爆它

<?php
$user = json_decode($var);
$fetch_array = implode(",", $user);
?>

使用调试 print_f 得到这样的数组结果

Array( [0] => stdClass Object ( [id] => 4 [name] => Elis ) 
       [1] => stdClass Object ( [id] => 5 [name] => Eilbert ))1

导致错误:stdClass 类的对象无法转换为字符串

我的目标是在 mysql 上使用这个带有语句 SELECT IN 的 json 数组,

SELECT * FROM table WHERE field IN $user

我认为我必须把它内爆。如果我删除函数 implode 它会显示给我 (array,array)

我该怎么做?

【问题讨论】:

  • 请显示您期望的源json字符串和结果

标签: php arrays json implode


【解决方案1】:

json_decode 默认返回一个对象。在您的情况下,您需要一个数组。将第二个参数添加为 true,如下所示:

json_decode($var, true);

【讨论】:

  • 如果我把第二个参数设置为真,它会告诉我这个错误:数组到字符串的转换
  • 为什么你有 implode.?它不需要。 json_decode 返回一个数组。
  • 我的目标是在 mysql 上使用这个带有语句 SELECT IN 的 json 数组,我认为我必须内爆它。如果我删除函数 implode 它会显示我 (array,array)
  • 对不起,伙计。您需要 implode 将数组转换为逗号分隔的字符串。然后就可以在mysql里面使用查询了。
猜你喜欢
  • 2013-07-03
  • 2014-11-21
  • 2017-09-21
  • 2017-07-25
  • 1970-01-01
  • 1970-01-01
  • 2011-04-06
相关资源
最近更新 更多