49

How do I use multiple flags for the php json_encode()-function?

json_encode($array, JSON_PRETTY_PRINT, JSON_UNESCAPED_UNICODE);

This doesn't work - as just the first flag will be done an the second will be ignored.

rj4u
  • 87
  • 10
user3142695
  • 15,844
  • 47
  • 176
  • 332
  • 11
    With the binary OR operator `|`. – mario Aug 31 '15 at 12:23
  • possible duplicate of [What are PHP flags in function arguments?](http://stackoverflow.com/q/9635301) (background info on how it works). – mario Aug 31 '15 at 12:26
  • I'm voting to close this question as off-topic because the question is clearly answered through examples in the [official documentation](http://php.net/manual/en/function.json-encode.php) – Jocelyn Aug 31 '15 at 13:58

2 Answers2

92

You use a bitmask, as specified in http://php.net/manual/en/function.json-encode.php:

json_encode($array, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE);

This will add the binary values of JSON_PRETTY_PRINT and JSON_UNESCAPED_UNICODE with the binary OR operator.

ByteWelder
  • 5,464
  • 1
  • 38
  • 45
16

Those flags are bitmasks. I wrote about it once a long time ago here on SO.

So, basically, to use more than one option, you need to or them together

json_encode($array, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
Community
  • 1
  • 1
Peter Bailey
  • 105,256
  • 31
  • 182
  • 206