2015-01-12 24 views
-1

我有一個$ photo_array,我想獲取特定的URL。如何通過php中的複雜數組進行解析

這是我的PHP。我敢肯定,我不是這樣做的權利:

$photo_array = $user_photos->asArray(); 

foreach($arr as $key => $value) 
{ 
    $url = $photo_array["data"]->$key->images->["0"]->source; 
    echo $url."<br>"; 
} 

這裏是我的數組的副本......見箭頭看到我想要的價值得到。 我怎樣才能做到在foreach必須適用於第一[0] ...所以我只想要1個URL中的每個關鍵

Array 
(
[data] => Array 
    (
     [0] => stdClass Object   <<<<< FOREACH applies to this key HERE 
      (
       [id] => 123 
       [created_time] => 2013-04-21T22:22:33+0000 
       [from] => stdClass Object 
        (
         [id] => 123 
         [name] => name 
        ) 

       [height] => 479 
       [icon] => https://fbstatic-a.akamaihd.net/rsrc.php/v2/yz/r/StEhvv3RhPvjk.gif 
       [images] => Array 
        (
         [0] => stdClass Object 
          (
           [height] => 1365 
    ***THIS IS WHAT I WANT >>>>> [source] => https://fbcdn-sphotos-b-a.akamaihd.net/hphotos-ak-xpf1/t31.0-8/9034014_10151555782563_1289220490_o.jpg 
           [width] => 2048 
          ) 

         [1] => stdClass Object 
          (
           [height] => 960 
           [source] => https://fbcdn-sphotos-b-a.akamaihd.net/hphotos-ak-xpf1/t31.0-8/p960x960/9032201_101513582563_1480490_o.jpg 
           [width] => 1440 
          ) 

         [2] => stdClass Object 
          (
           [height] => 720 
           [source] => https://fbcdn-sphotos-b-a.akamaihd.net/hphotos-ak-xpf1/t31.0-8/p720x720/903401_101513782563_148970_o.jpg 
           [width] => 1080 
          ) 

        ) 

       [link] => https://www.facebook.com/photo.php?fbid=10151351263&set=a.41902562.210698.6182562&type=1 
       [updated_time] => 2014-11-24T22:43:04+0000 
       [width] => 720 

那...感謝

回答

0
foreach ($photo_array['data'] as $photo) { 
    // iterate all images: 
    foreach ($photo->images as $image) { 
     echo $image->source; 
    } 

    // or just the first one: 
    echo $photo->images[0]->source; 
} 
+0

他只想要一個圖像url。 ''foreach必須應用於第一個[0] ...所以我只需要在每個鍵中輸入1個URL「' – Darren

0

你訪問錯誤。 dataarray,而不是object。這應該工作:

$url = $photo_array["data"][$key]->images['0']->source; 

同樣的道理也適用於images。這是一個array而不是一個object


  • 當訪問一個數組,你會直接索引(即[0])施加到陣列的名稱。
  • 訪問對象時,您可以通過->訪問密鑰(索引)。