Ahmad
Ahmad

Reputation: 4254

php json_encode issue in utf-8

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

Upvotes: 0

Views: 693

Answers (3)

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

Try this:

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

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

Hope it helps

Upvotes: 0

Jan Dragsbaek
Jan Dragsbaek

Reputation: 8101

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.

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798526

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

Upvotes: 1

Related Questions