2014-04-24 255 views
0

這是我原來的代碼循環通過陣列

[Width] => Array 
     (
      [0] => 400px 
      [1] => 323a 
     ) 

    [Height] => Array 
     (
      [0] => 244px 
      [1] => 244px 
     ) 

    [Captions] => Array 
     (
      [0] => [{ "captionId": 1,"caption": "Learning Clock Face is fun - you learn importance of time.","xyCordinates": "40px-250px","dimension": "70%-20%"},{"captionId": 2,"caption": "12 AM or 12 PM","xyCordinates": "40px-250px","dimension": "70%-20%"}] 
      [1] => [{ "captionId": 1,"caption": "Learning Clock Face is fun - you learn importance of time.","xyCordinates": "40px-250px","dimension": "70%-20%"},{"captionId": 2,"caption": "12 AM or 12 PM","xyCordinates": "40px-250px","dimension": "70%-20%"}] 
     ) 
) 

上面的代碼是我實際的代碼,現在我需要循環它使我的代碼類似於下面的一個。請幫助我。

頁面ID不斷遞增。請幫我出

"Pages": [ 
    { 
     "pageId": "1", 
     "resource": { 
     "width": "220px", 
     "height": "120px", 
     "captions": [ 
      { 
      "captionId": 1, 
      "caption": "Learning alphabets is fun - you learn what to eat.", 
      "xyCordinates": "40px-250px", 
      "dimension": "70%-20%" 
      } 
     ] 
     }, 
     "pageOrder": 1, 
     "state": 1 
    }] 
+0

我已經獲得了上面提到的代碼。我需要以一種我覺得很困難的方式進行格式化。所以請幫助我 – user1734770

回答

0

首先你必須要在該結構中的數據格式在PHP中,然後在你到底有沒有編碼爲JSON .. 你也可能需要讀取數據陣列和創建模型類,然後編碼爲JSON

json_encode() 

示例代碼

$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5); 

echo json_encode($arr); 

的出放將是 {「a」:1,「b」:2,「c」:3,「d」:4,「e」:5}