codeigniter 如何在数组中产生行距[duplicate]

tkqqtvp1  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(116)

此问题在此处已有答案

Pretty-Printing JSON with PHP(27个答案)
关闭5个月前.

$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"}
kfgdxczn

kfgdxczn1#

使用JSON_PRETTY_PRINT并且还需要使用echo "<pre>";
From PHP Manual:在返回的数据中使用空格来设置其格式。自PHP 5.4.0起可用

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

结果:

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

相关问题