27

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.

3
  • You are passing an array where a string is expected. You are creating the array in the line before - is that necessary? Why not just use $row->guide directly? Commented Sep 15, 2011 at 20:15
  • 1
    What are you trying to accomplish ? Commented Sep 15, 2011 at 20:20
  • What are you trying to do here? Commented Sep 15, 2011 at 20:40

6 Answers 6

55

I think you want json_encode, not json_decode.

Sign up to request clarification or add additional context in comments.

7 Comments

if use of json_encode my output is this: [{"guide":["\u0633\u06cc\u062f \u0633\u0639\u06cc\u062f \u062f\u0627\u062f\u0627\u0634\u0632\u0627\u062f\u0647"]},{"guide":["\u0633\u06cc\u062f \u0633\u0639\u06cc\u062f \u062f\u0627\u062f\u0627\u0634\u0632\u0627\u062f\u0647"]},{"guide":null}], they are persian word
@Selena: The \u0633 characters are just Unicode characters that are encoded. The first guide is سید سعید داداشزاده. Is that right?
@Selena: Show it where? If you echo the string (in JavaScript) they will print correctly. You don't need to do anything special, just echo them normally.
i not want echo they in JavaScript, What do you mean of just echo them normally!?
@Selena: What are you trying to do here? What do you want to do with the words?
|
34

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);

Comments

3

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();

Comments

3
  • Make an object

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

  • Show data from this $obj

    $obj->{'needed'};

Reference

Comments

1

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  ;

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.