11

When using echo json_encode($array, JSON_UNESCAPED_UNICODE);

I get the this error

Warning: json_encode() expects exactly 1 parameter, 2 given

Valentin Despa
  • 40,712
  • 18
  • 80
  • 106
Ben
  • 1,906
  • 10
  • 31
  • 47

2 Answers2

8

Your php version might be too low:

http://php.net/manual/en/function.json-encode.php

string json_encode ( mixed $value [, int $options = 0 ] )

5.3.0    The options parameter was added
biziclop
  • 14,466
  • 3
  • 49
  • 65
  • hmm ok and if this is the case how i can do same thing but with different script – Ben Mar 21 '12 at 09:16
  • 2
    At http://php.net/manual/en/function.json-encode.php there is a comment with "Simple replacement for JSON_UNESCAPED_UNICODE (PHP < 5.4 for example)" – biziclop Mar 21 '12 at 09:28
  • @EvenJohnson. `JSON_UNESCAPED_UNICODE` probably isn't actually necessary. If your version of PHP is too low, just do without it. – TRiG Nov 24 '12 at 17:14
  • 2
    Hmm, 5 upvotes and "This question is unlikely to help any future visitors" :) – biziclop Nov 08 '13 at 13:59
4

See patch at http://code.google.com/p/apns-php/issues/detail?id=22 which allows the same functionality on PHP 5.2.

Basically run something like this:

foreach ($array as &$val) {
    $val = preg_replace_callback('/\\\\u([0-9a-f]{4})/i',
        function($matches) {
            return mb_convert_encoding(pack('H*', $matches[1]), 'UTF-8', 'UTF-16');
        }, $val);
}
pevik
  • 4,523
  • 3
  • 33
  • 44
  • Even this comes close, this is technically wrong. Also PHP 5.4 has a bug here, see [PHP Bug #62010 json_decode produces invalid byte-sequences](https://bugs.php.net/bug.php?id=62010) – hakre Jun 20 '13 at 13:44