2016-02-29 49 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對象。