php json_decode() 期望参数 1 是字符串,给定数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7436925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:40:01  来源:igfitidea点击:

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

phpjson

提问by Jennifer Anthony

What causes this error in my code?

是什么导致我的代码中出现此错误?

$query = $this->db->query("SELECT * FROM tour_foreign ORDER BY id desc");
        $data = array();
        foreach ($query->result() as $row)
            $data[] = array('guide' => $row->guide);

            echo json_decode($data); //Line 167

error:

错误:

erro: json_decode() expects parameter 1 to be string, array given: Line Number: 167

UPDATE:

更新:

If I use json_encodeinstead of json_decode, my output is this:

如果我使用json_encode而不是json_decode,我的输出是这样的:

[{"guide":["\u0633\u06cc\u062f \u0633\u0639\u06cc\u062f \u062f\u0627\u062f\u0627\u0634\u0632\u0627\u062f\u0647"]},{"guide":["\u0633\u06c??c\u062f \u0633\u0639\u06cc\u062f \u062f\u0627\u062f\u0627\u0634\u0632\u0627\u062f\u0647"]},{"guide":null}]

They are persian words.

它们是波斯语。

回答by Rocket Hazmat

I think you want json_encode, not json_decode.

我想你想要json_encode,不是json_decode

回答by Kareem

Set decoding to true

将解码设置为

Your decoding is not set to true. If you don't have access to set the source to true. The code below will fix it for you.

您的解码未设置为 true。如果您无权将源设置为 true。下面的代码将为您修复它。

$WorkingArray = json_decode(json_encode($data),true);

回答by macintosh264

json_decode() is used to decode a json string to an array/data object. json_encode() creates a json string from an array or data. You are using the wrong function my friend, try json_encode();

json_decode() 用于将 json 字符串解码为数组/数据对象。json_encode() 从数组或数据创建一个 json 字符串。你用错了函数,我的朋友,试试 json_encode();

回答by Akhil Jain

here is the solution for similar problem which i was facing while extracting name from user profile facebook json object

这是我在从用户配置文件 facebook json 对象中提取名称时遇到的类似问题的解决方案

$uname=json_encode($userprof);
$uname=json_decode($uname);
echo "Welcome " . $uname -> name  ;

回答by Ayman Elshehawy

  • Make an object

    $obj = json_decode(json_encode($need_to_json));

  • Show data from this $obj

    $obj->{'needed'};

  • 做一个对象

    $obj = json_decode(json_encode($need_to_json));

  • 显示来自这个 $obj 的数据

    $obj->{'needed'};

Reference

参考