具有2个href属性的XML数组

时间:2010-04-06 19:15:07

标签: php

我有以下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属性,以便我可以将一个href作为<a>链接而将另一个作为<img>

如何输出每个href而不是两者?

目前正在尝试:

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

3 个答案:

答案 0 :(得分: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"

这会使用SimpleXMLElement的{​​{3}}方法。

我认为您不能直接访问属性($ elment-&gt; @attributes),因为这不是有效的语法。

答案 1 :(得分:0)

首先href - $ xml ['link'] [0] - &gt; attributes() - &gt; href

第二个href - $ xml ['link'] [1] - &gt; attributes() - &gt; href

答案 2 :(得分: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>
----

但您可能也对xsl(t)

感兴趣