Hasen
Hasen

Reputation: 12324

How to JSON encode an array with French accents?

There are many threads about this but none of them helped me solve this problem.

$array=array(
"dépendre"=>"to depend",
"dire"=>"to say",
"distraire"=>"distracted",
"être"=>"to be (being)",
);

Gets encoded like this with json_encode :

"d\u00e9pendre":"to depend","dire":"to say","distraire":"distracted","\u00eatre":"to be (being)"

So far I have tried this:

array_walk_recursive($array,function($value,$key) {
     $key = urlencode(utf8_decode($key));
});

Upvotes: 3

Views: 1694

Answers (2)

Michał Szczech
Michał Szczech

Reputation: 456

Please check the result from the following code:

<?php
$x=array(
    "dépendre"=>"to depend",
    "dire"=>"to say",
    "distraire"=>"distracted",
    "être"=>"to be (being)",
);

$encoded = json_encode($x);

var_dump($x);

var_dump(json_decode($encoded, true));

The string you get in your question is a correctly escaped JSON and could be successfully decoded.

Upvotes: 0

Mert Simsek
Mert Simsek

Reputation: 1560

Try this,

json_encode($array, JSON_UNESCAPED_UNICODE);

You should get this result;

{
  "dépendre":"to depend",
  "dire":"to say",
  "distraire":"distracted",
  "être":"to be (being)"
}

Upvotes: 6

Related Questions