Skip to content
Advertisement

How to add a line break in brackets and braces JSON PHP file?

I have the following code that generates data in json php:

JavaScript

Obtaining the following result:

JavaScript

But I need to be able to get a line break between the bracket and another line break for each brace, like this:

JavaScript

As this json data output is shown, what changes should I use in my PHP JSON code to obtain the same result.

Advertisement

Answer

HTML

If you are likely to be displaying the resulting JSON to users in a web browser, you can simply use: echo "[<br>"; and echo "<br>]"; to insert html linebreaks.

API/REST Script

If the result is being handled by any other application other than a web browser, use:

  • echo "[ n"; and echo "n ]"; for programs on Linux/Unix Based Systems (Including MacOS 10+)
  • echo "[ r"; and echo "r ]"; for programs on MacOS Versions Lower than 9
  • echo "[ rn"; and echo "rn ]"; for programs on Windows Based Systems

The backslash is the universal “escape” character so if you are unsure about which platform you will be running the script on or want the output to be universal regardless of OS, use the windows format of ‘rn’ and each OS will work it’s own magic

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement