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_encode
instead of json_decode
, my output is this:
[{"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.
I think you want json_encode
, not json_decode
.
here is the solution for similar problem which i was facing while extracting name from user profile facebook json object
$uname=json_encode($userprof);
$uname=json_decode($uname);
echo "Welcome " . $uname -> name ;
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.
$WorkingArray = json_decode(json_encode($data),true);
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();
Ok I was running into the same issue. What I failed to notice is that I was using json_decode() instead of using json_encode() so for those who are going to come here please make sure you are using the right function, which is json_encode()
Note: Depends on what you are working on but make sure you are using the right function.
Make an object
$obj = json_decode(json_encode($need_to_json));
Show data from this $obj
$obj->{'needed'};
Source: Stackoverflow.com