2010-04-06 6 views
0

나는 다음과 같은 XML 배열이 : 나는 <a>로 한 HREF을 넣을 수 있도록 더 큰 배열 내부의XML 배열 속성

["link"]=> 
      array(2) { 
      [0]=> 
      object(SimpleXMLElement)#311 (1) { 
       ["@attributes"]=> 
       array(3) { 
       ["type"]=> 
       string(9) "text/html" 
       ["href"]=> 
       string(48) "http://twitter.com/bob/statuses/1226112723" 
       ["rel"]=> 
       string(9) "alternate" 
       } 
      } 
      [1]=> 
      object(SimpleXMLElement)#312 (1) { 
       ["@attributes"]=> 
       array(3) { 
       ["type"]=> 
       string(9) "image/png" 
       ["href"]=> 
       string(59) "http://a3.twimg.com/profile_images/226895523/Dan_normal.png" 
       ["rel"]=> 
       string(5) "image" 
       } 
      } 
      } 

이, 내가 처음을받을 필요가 있고 두 번째 HEF가 각각 따로 속성을 링크가 있고 다른 하나는 <img>입니다.

두 href를 함께 출력하지 않고 어떻게 출력 할 수 있습니까?

현재이 시도 :

foreach($entry->link as $link) { 
    echo $link->attributes()->href; 
} 
+0

에 관심이있을 수있는 당신 보여 주었다? 현재 어떤 루프가 있는지 분명하지 않기 때문에 더 많은 코드를 붙여 넣을 수 있습니까? 감사. –

+0

다음과 같은 문자열을 얻을 수 있습니다 : http://twitter.com/bob/statuses/1226112723http://a3.twimg.com/profile_images/226895523/Dan_normal.png concatonated – CLiown

+0

출력물을'$ 'var_dump ($ xml); 대신'xml-> asxml()'을 사용하십시오. 또는 http://api.twitter.com/1/favorites/bob.atom에서 simplexml을 통해 피드를 구문 분석한다고 가정 해 보겠습니다. (왜냐하면 그게 당신이하는 일이기 때문입니다.) – VolkerK

답변

0
$url = 'http://api.twitter.com/1/favorites/bob.atom'; 
$feed = simplexml_load_file($url); 

$testStop = 0; 
foreach($feed->entry as $entry) { 
    echo 'title: ', $entry->title, "\n"; 

    // store all link/@href in a hashtable 
    // so you can access them in any order you like 
    // without resorting to xpath or alike. 
    $links = array(); 
    foreach($entry->link as $link) { 
    $links[(string)$link['rel']] = (string)$link['href']; 
    } 

    if (isset($links['image'])) { 
    echo '<img src="', $links['image'], '" />', "\n"; 
    } 
    if (isset($links['alternate'])) { 
    echo '<a href="', $links['alternate'], '" />alternate</a>', "\n"; 
    } 
    echo "----\n"; 
    if (2 < ++$testStop) die; 
} 

은 (현재)

title: kolchak: Sometimes I think I need a new butler. But then it's like "Nah, he's still got one thumb. We good." 
<img src="http://a1.twimg.com/profile_images/668496250/Picture_14_normal.jpg" /> 
<a href="http://twitter.com/kolchak/statuses/10648055680" />alternate</a> 
---- 
title: shitmydadsays: "War hero? No. I was a doc in Vietnam. My job was to say "This is what happens when ." 
<img src="http://a3.twimg.com/profile_images/362705903/dad_normal.jpg" /> 
<a href="http://twitter.com/shitmydadsays/statuses/10580558323" />alternate</a> 
---- 
title: shitmydadsays: "I lost 20 pounds...How? I drank bear piss and took up fencing. How the you think, son? I exercised." 
<img src="http://a3.twimg.com/profile_images/362705903/dad_normal.jpg" /> 
<a href="http://twitter.com/shitmydadsays/statuses/10084782056" />alternate</a> 
---- 

를 인쇄하지만 당신은 또한 루프는 ECHO에서 얻을 출력이 무엇 xsl(t)

0

당신은 일반 배열/개체 액세스를 사용하여 href 속성에 액세스 할 수 있습니다. 그냥 나중에 사용할 수 있도록 배열에 저장 :

$hrefs = array(); 

foreach($array['links'] as $links) { 
    foreach($links->attributes as $key>=$value) { 
     if('href' == $key) { 
      $hrefs[] = $value; 
     } 
    } 
} 

// $href[0] = "http://twitter.com/bob/statuses/1226112723" 
// $href[1] = "http://a3.twimg.com/profile_images/226895523/Dan_normal.png" 

SimpleXMLElementattributes() 방법을 사용합니다.

유효한 구문이 아니기 때문에 직접 속성에 액세스 할 수 있다고 생각하지 않습니다 ($ elment -> @ attributes). 우선 HREF

0

- $ XML을이 [ '링크'] [0] ->()는 특성 -> HREF

둘째 HREF - $ XML을 [ '링크'] [1] ->)는 (특성 -> HREF