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

2022-08-30 19:23:34

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

$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

错误:

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

更新:

如果我使用 代替 ,我的输出是这样的:json_encodejson_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}]

它们是波斯语单词。


答案 1

我想你想要json_encode,而不是json_decode


答案 2

将解码设置为 true

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

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

推荐