打印没有<pre> tag

时间:2015-12-13 22:59:02

标签: php arrays pre formatted

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.

3 个答案:

答案 0 :(得分:5)

您可能想要设置除Note myNote = new Note( txt_Title.getText().toString(), foreignKey, personType, txt_Body.getText().toString() ); tbl_notes.addNewNote(myNote); 以外的mime类型。试试text/html(例如,当使用带text/plain的html时)或<pre>(json的官方类型)

在PHP中,使用以下行:

application/json

答案 1 :(得分:1)

这是JSON,请尝试echo json_encode($array);

编辑:对于格式化结果,json_encode采用第二个参数。

试试这个echo json_encode($array, JSON_PRETTY_PRINT);

答案 2 :(得分:0)

如果您只想在元素之间添加换行符,可以尝试:

echo implode("<br>", $array);