Search code examples
phparraysformatted

Print pre formatted Array without <pre> tag


here is my problem:

I want output a formatted array on my webserver, like this:

{
    "status": true,
    "motd": "    [  Mineplex Games  ]    \n        \u2744 New Game \u2744 Gladiators \u2744",
    "version": "1.8",
    "players": {
        "online": 24410,
        "max": 24411
    },
    "ping": "0.039",
    "cache": 1450047242
}

In the source of that page there is not <pre> tag, but I don't know how to reproduce that result without that tag.

How can I do that? Here is my code

  echo '<pre>';
  print_r($array);
  echo '</pre>';

EDIT: echo json_encode($array); will output a non-formatted text, just a line.


Solution

  • You probably want to set a mime-type other than text/html. try text/plain (will show like when used html with <pre>) for example or application/json (the official type for json)

    In PHP, use the following lines:

    header('Content-Type: application/json');
    echo json_encode($array, JSON_PRETTY_PRINT);