如何在数组中产生一行空格

12 浏览
0 Comments

如何在数组中产生一行空格

这个问题已经有答案了:

使用PHP漂亮地输出JSON

$arr = array(
   'toemail'=>$v->agent_primary_email,
   'agentname'=>$v->agent_firstname,
   'agentid'=>$v->agent_id,
   'subject'=>'The details of total number of properties saved by your clients',
   'totalprop'=>$v->prop_count
);
echo json_encode($arr);exit;

输出结果看起来像这样

{"toemail":"abc@gmail.com","agentname":"john","agentid":"110012","subject":"The    details of total number of properties saved by your clients","totalprop":"131"}

但我需要做出什么改变,才能使输出结果看起来像这样

{"toemail":"abc@gmail.com",
 "agentname":"john",
 "agentid":"110012",
 "subject":"The details of total number of properties saved by your                     clients",
 "totalprop":"131"}

admin 更改状态以发布 2023年5月21日
0
0 Comments

使用JSON_PRETTY_PRINT,并且需要使用echo "

";

来自PHP手册:使用返回的数据中的空格来格式化数据,自PHP 5.4.0起可用。

$array = array(
    'test'=>1,
    'test2'=>'test',
    'test3'=>'test 3'
);
echo "
";
echo json_encode($array,JSON_PRETTY_PRINT);

结果:

{
    "test": 1,
    "test2": "test",
    "test3": "test 3"
}

0