PHP, json_encode and special characters?!?!

I’m trying to figure this out…

$users = array('Peter Jørgensen', 'Glen Thompson', 'Kit Anderson');
$checkuser = array();

foreach ($users as $user){
	$checkuser[] = array($user);
}

echo json_encode($checkuser)

;

This prints out:
[[“Peter J\u00f8rgensen”],[“Glen Thompson”],[“Kit Anderson”]]

As you can see the “Jørgensen” comes out like “J\u00f8rgensen”… How do I get it to show/print this correct???

Please HELP!!!

1 Like

That’s just a unicode character code. Try using another encoding script, possibly the Zend one:

http://stackoverflow.com/questions/410704/cyrillic-characters-in-phps-jsonencode

Read somewhere else that I could utf8_encode’ing the string, but not sure where, when and how… Please help!

Did you try reading the comments on the php.net json_encode page?

That is printing correctly. Anything that consumes JSON (JavaScript or any other client library like PHP’s JSON extension) should handle those “special” (they’re not really special, just not ASCII) characters without any problems.

FYI, I think you might have been asking about [fphp]iconv[/fphp].

I have tried to surf around the suggested websites and googled it on my own, but still can’t get this to work…

I have tried this:

$users = array('Peter Jørgensen', 'Glen Thompson', 'Kit Anderson');
$encodedUsers = array_map(utf8_encode, $users);

This only makes it like this:
[[“Peter J\u00c3\u00b8rgensen”],[“Glen Thompson”],[“Kit Anderson”]]

If I use utf8_decode, then the first name comes out as [“null”]…

Its tearing me apart… Please help… I am so close to cry :expressionless:

As Salathe said this is just an encoding problem -

see http://www.sitepoint.com/forums/showthread.php?t=647194

It is correct. Why do you want it to be different?

Well… How do you mean it comes out correct???

It’s in accordance the json specs. While it would also be allowed to print the characters directly, the php-extension chooses to encode non-ascii characters as their unicode points to ensure safe transport. Any standards-compliant json-parser will know how to interpret this.