41

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
  • 77
  • 10
user3142695
  • 13,907
  • 34
  • 141
  • 281
  • 8
    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

78

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.

Byte Welder
  • 5,174
  • 1
  • 36
  • 43
13

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
  • 103,526
  • 30
  • 178
  • 200