【问题标题】:How to get the value for the given json and get result如何获取给定 json 的值并获取结果
【发布时间】:2015-03-12 01:34:27
【问题描述】:

我正在尝试获取以下查询的结果

$users = DB::select("SELECT FIND_IN_SET('l','a,b,c,d') as Res");

当我这样做的时候

返回 $users;

这是我的 json

[{"Res":0}]

当我尝试decode 时,它显示错误

json_decode() expects parameter 1 to be string, array given

当我var_dump 时,我会变得像

array(1) { [0]=> object(stdClass)#773 (1) { ["Res"]=> int(0) } }

那么,我怎样才能得到'Res' 的结果?

【问题讨论】:

  • 猜你需要做 $object[ 0 ]->res;因为您有一个数组,其中包含一个对象,该对象包含“res”属性中的字符串。
  • edit 在此处添加遇到这些问题的代码的完整上下文。发布 Minimal, Complete, Verifiable Example 来证明您的问题将帮助您获得更好的答案。谢谢!
  • 我的问题更新了,请看

标签: php arrays json decode encode


【解决方案1】:

解决方法在错误中:

json_decode() expects parameter 1 to be string, **array given**

也就是说,结果数据作为数组返回,该数组基于您的 var_dump 还包含您的结果对象和后续数据。

应该这样做:

<?php
     $data = $users[0]->Res
     $decoded = json_decode($data);

请注意,这实际上只是将您的 JSON 字符串转换为对象。如果愿意,您可以使用第二个参数将其作为数组返回:

<?php 
     $data = $users[0]->Res
     $decoded = json_decode($data, true);

【讨论】:

    猜你喜欢
    • 2018-01-22
    • 1970-01-01
    • 1970-01-01
    • 2015-04-23
    • 2012-09-10
    • 1970-01-01
    • 1970-01-01
    • 2016-06-19
    • 1970-01-01
    相关资源
    最近更新 更多