2016-04-14 88 views
1

我確定我錯過了一些非常簡單的東西,但我試圖將一個feed轉換爲一個漂亮的乾淨的關聯數組,以便在我的視圖中引用。php數組鍵值對的問題

這裏是環(有一些文本格式分割他們):

$headers = get_headers('https://s3-us-west-1.amazonaws.com/sg-retail/' . $file); 
     $metas = []; 

foreach($headers as $meta) { 
      $metaKey = strtok($meta, ':'); 
      $metaVal = ltrim(strstr($meta, ':'), ':'); 
      array_push($metas, [$metaKey => $metaVal]); 
     } 

我得到這樣的結果:

[{"HTTP\/1.1 200 OK":""},{"x-amz-id-2":" sNsiGT+p8eZaFJ3RxHKLe\/vN4BfJ27Zp6baI+OvXr+9VqSosNfpSfj73b0XnQAEXKsNgTzBSaM4="},{"x-amz-request-id":" 50EE32CE562BDBE1"},{"Date":" Thu, 14 Apr 2016 23:15:03 GMT"},{"x-amz-meta-featured":" Featured Style: DR349"},{"x-amz-meta-postcopy":" Choose a ring as unique as the woman wearing it, with help from @SimonGJewelry!"},{"x-amz-meta-title":" Friday, April 1"},{"x-amz-meta-hashtags":" #Ring #Jewelry #JewelryGram #EngagementRing #Style #Diamonds #HeAsked #SheSaidYes #Love #Wedding #WeddingInspo #SimonG #SimonGJewelry"},{"Last-Modified":" Thu, 14 Apr 2016 18:55:03 GMT"},{"ETag":" \"7042f7d9383e180d9ed8516d2df0428f\""},{"Accept-Ranges":" bytes"},{"Content-Type":" image\/jpeg"},{"Content-Length":" 499591"},{"Server":" AmazonS3"},{"Connection":" close"}] 

這似乎沒什麼問題,但無論是我遲鈍或I沒有正確格式化這些嵌套數組。

這工作:

return $twitter_posts[0]["metas"]; 

當我試圖讓通過鍵特定的變量:

return $twitter_posts[0]["metas"]["x-amz-meta-postcopy"]; 

這teels我%& $(關:

undefined index 

編輯:每個請求的整個功能(也許不相關,但在這裏你去):

函數調用:

$twitter_posts = \App\Asset::fetch('toolkit/social-media/twitter/post-images/'); 

功能:

public static function fetch($path) 
{ 
    $files = Storage::files($path); 
    $data = []; 

    foreach($files as $file) { 
     $headers = get_headers('https://s3-us-west-1.amazonaws.com/sg-retail/' . $file); 
     $metas = []; 

     foreach($headers as $meta) { 
      $metaKey = strtok($meta, ':'); 
      $metaVal = ltrim(strstr($meta, ':'), ':'); 
      array_push($metas, [$metaKey => $metaVal]); 
     }   

     $array = ['image' => 'https://s3-us-west-1.amazonaws.com/sg-retail/' . $file, 'metas' => $metas]; 
     array_push($data, $array); 
    } 

    return $data; 
} 

這一呼籲:

return var_dump($twitter_posts[0]["metas"]); 

得到這樣的結果:

array(15) { 
     [0]=> 
     array(1) { 
     ["HTTP/1.1 200 OK"]=> 
     string(0) "" 
     } 
     [1]=> 
     array(1) { 
     ["x-amz-id-2"]=> 
     string(77) " cJgthWyhsfIdX5zgNAmS6fp05iYv7gKt4dhThGtItV5QPv5MgLxYsRCfQ8uEwwuWmsSTWSULE5c=" 
     } 
     [2]=> 
     array(1) { 
     ["x-amz-request-id"]=> 
     string(17) " 2C043CB5EDF8F423" 
     } 
     [3]=> 
     array(1) { 
     ["Date"]=> 
     string(30) " Thu, 14 Apr 2016 23:33:38 GMT" 
     } 
     [4]=> 
     array(1) { 
     ["x-amz-meta-featured"]=> 
     string(22) " Featured Style: DR349" 
     } 
     [5]=> 
     array(1) { 
     ["x-amz-meta-postcopy"]=> 
     string(80) " Choose a ring as unique as the woman wearing it, with help from @SimonGJewelry!" 
     } 
     [6]=> 
     array(1) { 
     ["x-amz-meta-title"]=> 
     string(16) " Friday, April 1" 
     } 
     [7]=> 
     array(1) { 
     ["x-amz-meta-hashtags"]=> 
     string(134) " #Ring #Jewelry #JewelryGram #EngagementRing #Style #Diamonds #HeAsked #SheSaidYes #Love #Wedding #WeddingInspo #SimonG #SimonGJewelry" 
     } 
     [8]=> 
     array(1) { 
     ["Last-Modified"]=> 
     string(30) " Thu, 14 Apr 2016 18:55:03 GMT" 
     } 
     [9]=> 
     array(1) { 
     ["ETag"]=> 
     string(35) " "7042f7d9383e180d9ed8516d2df0428f"" 
     } 
     [10]=> 
     array(1) { 
     ["Accept-Ranges"]=> 
     string(6) " bytes" 
     } 
     [11]=> 
     array(1) { 
     ["Content-Type"]=> 
     string(11) " image/jpeg" 
     } 
     [12]=> 
     array(1) { 
     ["Content-Length"]=> 
     string(7) " 499591" 
     } 
     [13]=> 
     array(1) { 
     ["Server"]=> 
     string(9) " AmazonS3" 
     } 
     [14]=> 
     array(1) { 
     ["Connection"]=> 
     string(6) " close" 
     } 
    } 
+1

我們需要在這裏看到更多的代碼是...函數內的第一個代碼塊?我們可以看到函數調用嗎?看起來你有一個名爲$ metas的數組,然後突然另一個叫$ twitter_posts。你是怎麼到達那裏的? – larsAnders

+0

'return $ twitter_posts [0] ['metas']'??這是從哪裏來的? – Marcus

+0

@larsAnders確定我添加了整個功能。這是一種將S3對象元數據與對象的URL一起解析爲laravel應用程序的方法 –

回答

2

你的陣列巢編輯太深了。相反array_push的,你可以使用$array[$key]語法來得到你想要的格式,所以不是這樣的:

array_push($metas, [$metaKey => $metaVal]); 

,你可以這樣做:

$metas[$metaKey] = $metaVal; 
+0

BOOM!我們有一個贏家,謝謝。知道它是這樣的 –

+0

沒問題。請注意上面有關使用json_decode()分析JSON的註釋。 – larsAnders