2016-02-29 50 views
2
$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":"[email protected]","agentname":"john","agentid":"110012","subject":"The details of total number of properties saved by your clients","totalprop":"131"} 

但我应该有什么样的变化使,从而使输出看起来像这样

{"toemail":"[email protected]", 
"agentname":"john", 
"agentid":"110012", 
"subject":"The details of total number of properties saved by your      clients", 
"totalprop":"131"} 
+0

我的意思是我想在每个对象中有一个新行的输出,而不是一行中的所有内容。 –

+2

如果它只是用于输出样式的目的,只需使用'JSON_PRETTY_PRINT'标志。它已经在[手册](http://php.net/manual/en/function.json-encode.php) – Ghost

回答

3

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

谢谢@devpro ... –

+0

@KLP:如果它的工作,不要忘记接受这个回答....这将有助于他人.http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – devpro

0

添加JsonView 插件在Chrome查看格式化的JSON对象。