0

I have following array in php

$array = array(
    array('name'=>'abc', 'text'=>'اسلسصثصض صثصهخه عه☆anton & budi☆' ),
    array('name'=>'xyz', 'text'=>'nice' ),
);

when i use json_encode, the result is different :

[
  {
    "name": "abc",
    "text": "\u0627\u0633\u0644\u0633\u0635\u062b\u0635\u0636 \u0635\u062b\u0635\u0647\u062e\u0647 \u0639\u0647\u2606anton '<&>' budi\u2606"
   },
   {
    "name": "xyz",
    "text": "nice"
  }
]

why the result is not like this ?

[
    {
         "name": "abc",
         "text": "اسلسصثصض صثصهخه عه☆anton &amp; budi☆"
     },
     {
         "name": "xyz",
         "text": "nice"
    }
]

Thanks

0

3 Answers 3

1

Because PHP doesn't assume/allow a non-ASCII character set when encoding. Both results are equivalent when decoded.

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

Comments

1

Because it is encoded to JSON. With your first output, you are absolutely 100% certain that the browser will receive what you want it to. With the second, you cannot be sure.

Comments

0

Try this:

//for encode
json_encode(array_map('base64_encode', $array));

//and for decode
array_map('base64_decode', json_decode($array));

Hope it helps

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.