2014-06-19 98 views
-1

從PHP數組到一個JSON數組是可能的嗎?如果是這樣,有人可以進一步解釋一下嗎?將一個PHP數組格式化爲JSON數組

events: [ 
    { 
     title: 'Test event', 
     location: 'Test location', 
     start: { 
      date: '20140607', time: '17.00' 
     }, 
     end: { 
      date: '20140623', time: '17.00' 
     } 
    }, 
    { 
     title: 'Test event', 
     location: 'Test location', 
     start: { 
      date: '20140607', time: '17.00' 
     }, 
     end: { 
      date: '20140623', time: '17.00' 
     } 
    } 
], 

這是我到目前爲止有:

<?php 
    $json->title = 'Test event'; 
    $json->location = 'Test location'; 

    echo json_encode($json); 
?> 

{"title":"Test event","location":"Test location"} 
+0

?這個問題非常模糊,你已經得到了你想要的東西? – EaterOfCode

+1

當你使用'json_encode()'時,你的JSON有什麼問題嗎? –

+0

我想他想要一個漂亮的格式化功能。 –

回答

2

你在正確的軌道上,但去進一步你必須使它成爲多維的(你可以把它寫成一個對象,但我更喜歡陣列):

<?php 
$jsonArr['title'] = 'Test event'; 
$jsonArr['location'] = 'Test location'; 

$jsonArr['start']['date']= '20140607'; 
$jsonArr['start']['time']= '17.00'; 

$jsonArr['end']['date']= '20140607'; 
$jsonArr['end']['time']= '17.00'; 

echo json_encode($jsonArr); 

//Or an object example: 

$json->title = 'Test event'; 
$json->location = 'Test location'; 

$json->start->date= '20140607'; 
$json->start->time= '17.00'; 

$json->end->date= '20140607'; 
$json->end->time= '17.00'; 

echo json_encode($json); 

?> 

{"title":"Test event","location":"Test location","start":{"date":"20140607","time":"17.00"},"end":{"date":"20140607","time":"17.00"}} 

這應該對你很好

+0

非常感謝 - 這很有道理。 –

+0

我的意思是問,多個事件呢?你能展示一個對象的例子嗎?只是試圖讓我的頭在附近。 –

+0

你是什麼意思多個事件?我將在 – JammyDodger231

1

可以使用JSON_PRETTY_PRINT選項json_encode()

<?php 
    $json = new stdClass(); 
    $json->title = 'Test event'; 
    $json->location = 'Test location'; 

    echo json_encode($json, JSON_PRETTY_PRINT); 
?> 

編輯:

如何我會做開始{},結束{}區域,並將其放入數組中嗎?

可以定義變量是這樣的:

$json = array(
    'events' => array(
    array(
     'title' => 'Test event', 
     'location' => 'Test location', 
     'start' => array(
     'date' => '20140607', 
     'time' => '17.00' 
    ), 
     'end' => array(
     'date' => '20140623', 
     'time' => '17.00' 
    ) 
    ), 
    array(
     'title' => 'Test event', 
     'location' => 'Test location', 
     'start' => array(
     'date' => '20140607', 
     'time' => '17.00' 
    ), 
     'end' => array(
     'date' => '20140623', 
     'time' => '17.00' 
    ) 
    ) 
) 
); 

它輸出:

{ 
    "events": [ 
     { 
      "title": "Test event", 
      "location": "Test location", 
      "start": { 
       "date": "20140607", 
       "time": "17.00" 
      }, 
      "end": { 
       "date": "20140623", 
       "time": "17.00" 
      } 
     }, 
     { 
      "title": "Test event", 
      "location": "Test location", 
      "start": { 
       "date": "20140607", 
       "time": "17.00" 
      }, 
      "end": { 
       "date": "20140623", 
       "time": "17.00" 
      } 
     } 
    ] 
} 
+0

謝謝!所以,如果我要把它包含在JS腳本函數中,這是否是最好的方法呢? http://snippi.com/s/8z9wq4t –

+0

如果事件來自數據庫,那麼是的。但是如果數據是靜態的(包含在源文件中),那麼你可以在沒有PHP的情況下完成。在這種情況下,只需使用JavaScript:http://jsfiddle.net/r2nAe/1/ –

+0

事件來自數據庫,因此很好知道。謝謝! –