2011-11-20 43 views
2

我有以下數據,我從一個XML文件解析,現在我有一個問題,返回數據。如何從照片數組中的陣列中返回照片src數據。任何關於我在做什麼的想法都是錯誤的?如何從陣列中的數組返回數據

代碼

$xml = simplexml_load_file($url); 
$photo_url = $xml['photo']->src; 

for ($i=0, $n=count($photo_url); $i<$n; ++$i) { 
    echo $photo_url[$i].'<br>'; 
} 

數據

["photo"]=> 
    array(46) { 
    [0]=> 
    object(SimpleXMLElement)#2 (1) { 
     ["@attributes"]=> 
     array(6) { 
     ["id"]=> 
     string(5) "26001" 
     ["src"]=> 
     string(36) "1006416.jpg" 
     ["thumb"]=> 
     string(42) "1006416_thumb.jpg" 
     ["title"]=> 
     string(16) "album" 
     ["subtitle"]=> 
     string(6) "01.jpg" 
     ["favorite"]=> 
     string(0) "" 
     } 
    } 
    [1]=> 
    object(SimpleXMLElement)#3 (1) { 
     ["@attributes"]=> 
     array(6) { 
     ["id"]=> 
     string(5) "26001" 
     ["src"]=> 
     string(36) "1006417.jpg" 
     ["thumb"]=> 
     string(42) "1006417_thumb.jpg" 
     ["title"]=> 
     string(16) "album" 
     ["subtitle"]=> 
     string(6) "02.jpg" 
     ["favorite"]=> 
     string(0) "" 
     } 
    } 

回答

1

您應該使用SimpleXMLElement attributes方法,像這樣:

$xml = simplexml_load_file($url); 
foreach($xml as $xml_node) 
{ 
    $attributes = $xml_node->attributes(); 
    echo 'Photo source: ' . $attributes['src'] . "\n"; 
} 

Demo

+0

哇!非常感謝 – bammab