Json_decode () expects parameter 1 to be the string given by the array

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 

mistake:

 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 will be like 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}] 

These are Persian words.

+13
json php
source share
5 answers

I think you want json_encode , not json_decode .

+42
source share

Set decoding to true

Your decoding is not set to true. If you do not have access to the source, set to true. The code below will fix this for you.

 $WorkingArray = json_decode(json_encode($data),true); 
+14
source share

json_decode () is used to decode a json string for 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 ();

+2
source share

You need to set to true. If you do not have access to the source, encode and decode it.

 $answer = json_decode(json_encode( $_GET['json'] ), true ); 
+2
source share

here is a solution for a similar problem that i encountered while extracting the name from facebook user profile json object

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

All Articles