【问题标题】:How to produce a line space in arrays如何在数组中产生行间距
【发布时间】:2016-06-12 04:13:33
【问题描述】:
$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"}

【问题讨论】:

  • 我的意思是我希望输出在每个对象中都有一个新行,而不是在一行中。
  • 如果它只是用于输出样式,只需使用JSON_PRETTY_PRINT 标志。它已经在manual

标签: php arrays json codeigniter associative-array


【解决方案1】:

使用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"
}

【讨论】:

猜你喜欢
  • 2019-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-09
  • 2018-03-17
  • 1970-01-01
  • 1970-01-01
  • 2020-12-15
相关资源
最近更新 更多