2016-10-21 99 views
0

我已經搜索了高和低,我必須忽略明顯的東西。我怎樣才能重構我從youtube API檢索到的輸出,看起來像下面的第二個代碼塊。另外我想刪除陣列的視頻部分。我甚至不知道從哪裏開始,或者我會發布一些迄今爲止我嘗試過的示例。如何重新格式化此數組?

我還想知道第一個數組的構建方式和第二個數組的構造術語。

以下是從我們的API調用中返回的youtube數組。這僅僅是來自YouTube的Feed中的2個帖子。他們從「項目」下開始:[陣列的一部分。

Array 
(
    [videos] => { 
"kind": "youtube#playlistItemListResponse", 
"etag": "\"I_8xdZu766_FSaexEaDXTIfEWc0/qYy5QZyPpmtNPAJ_cTTDagaLYv0\"", 
"nextPageToken": "CAIQAA", 
"pageInfo": { 
    "totalResults": 15, 
    "resultsPerPage": 2 
}, 
"items": [ 
    { 
    "kind": "youtube#playlistItem", 
    "etag": "\"I_8xdZu766_FSaexEaDXTIfEWc0/o2RZKiVOV3D5PlBUVONXwxRQACQ\"", 
    "id": "VVUtU1dDTm1QOS11ODFLOC1GMkY5RlB3LmJITEwxTDlFX0pB", 
    "snippet": { 
    "publishedAt": "2015-09-23T21:52:41.000Z", 
    "channelId": "UC-SWCNmP9-u81K8-F2F9FPw", 
    "title": "A day in the life of a sponsored skateboarder/parent", 
    "description": "I've had a hard time connecting with my family lately.", 
    "thumbnails": { 
    "standard": { 
     "url": "https://i.ytimg.com/vi/bHLL1L9E_JA/sddefault.jpg", 
     "width": 640, 
     "height": 480 
    } 
    }, 
    "channelTitle": "Tony Hawk", 
    "playlistId": "UU-SWCNmP9-u81K8-F2F9FPw", 
    "position": 0, 
    "resourceId": { 
    "kind": "youtube#video", 
    "videoId": "bHLL1L9E_JA" 
    } 
    } 
    }, 
    { 
    "kind": "youtube#playlistItem", 
    "etag": "\"I_8xdZu766_FSaexEaDXTIfEWc0/tFkAXp2IF-WC4lWu_OsWEF4mLIk\"", 
    "id": "VVUtU1dDTm1QOS11ODFLOC1GMkY5RlB3LkwzdHZvTnBBc25J", 
    "snippet": { 
    "publishedAt": "2015-01-05T05:13:22.000Z", 
    "channelId": "UC-SWCNmP9-u81K8-F2F9FPw", 
    "title": "Tahitian treats", 
    "description": "", 
    "thumbnails": { 
    "standard": { 
     "url": "https://i.ytimg.com/vi/L3tvoNpAsnI/sddefault.jpg", 
     "width": 640, 
     "height": 480 
    } 
    }, 
    "channelTitle": "Tony Hawk", 
    "playlistId": "UU-SWCNmP9-u81K8-F2F9FPw", 
    "position": 1, 
    "resourceId": { 
    "kind": "youtube#video", 
    "videoId": "L3tvoNpAsnI" 
    } 
    } 
    } 
] 
} 

) 

我想重新格式化它,使它看起來更像這樣。爲了不必重新輸入全部內容,我只添加了一些字段。希望瞭解基本想法。

Array 
(
[data] => Array 
     (
     [0] => stdClass Object 
      (
      [kind] => "youtube#playlistItem", 
      [etag] => "\"I_8xdZu766_FSaexEaDXTIfEWc0/o2RZKiVOV3D5PlBUVONXwxRQACQ\"", 
      [id] => "VVUtU1dDTm1QOS11ODFLOC1GMkY5RlB3LmJITEwxTDlFX0pB", 
      [snippet] => (
       [publishedAt] => "2015-09-23T21:52:41.000Z", 
       [channelId] => "UC-SWCNmP9-u81K8-F2F9FPw", 
      ) 
      ) 
     [1] => stdClass Object 
      (
      [kind] => "youtube#playlistItem", 
      [etag] => "\"I_8xdZu766_FSaexEaDXTIfEWc0/o2RZKiVOV3D5PlBUVONXwxRQACQ\"", 
      [id] => "VVUtU1dDTm1QOS11ODFLOC1GMkY5RlB3LmJITEwxTDlFX0pB", 
      [snippet] => (
       [publishedAt] => "2015-09-23T21:52:41.000Z", 
       [channelId] => "UC-SWCNmP9-u81K8-F2F9FPw", 
      ) 
      ) 
     ) 
) 

回答

0

當然,我忘了解碼JSON響應...

json_decode($視頻);

上面的第一個代碼塊是一個json數組,第二個塊是解碼版本。