2 href nitelikleri ile XML dizi

3 Cevap

Ben aşağıdaki XML dizi var:

["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"
              }
            }
          }

Bu daha büyük bir dizi içeride, ben bir <img> ile <a> bağlantı ve başka olarak bir href koyabilirsiniz böylece seperatly birinci ve ikinci hef niteliğini almak gerekir.

Nasıl çıktı alabilirsiniz her href ziyade birlikte hem daha?

Şu anda bu çalışıyor:

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

3 Cevap

$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;
}

(Şu anda) baskılar

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>
----

Ama aynı zamanda xsl(t) ilginizi çekebilir

Sen erişebilirsiniz href, normal dizi / nesne erişimini kullanarak bağlıyor. Sadece daha sonra kullanmak üzere bir dizi saklayın:

$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"

Bu SimpleXMLElement 'nin kullanımı attributes() bir yöntem yapar.

Ben bu geçerli sözdizimi değil gibi ($ Elment-> @ attributes) doğrudan özelliklerini erişebilirsiniz sanmıyorum.

İlk href - $ xml ['bağlantı'] [0] -> attributes () -> href

İkinci href - $ xml ['bağlantı'] [1] -> attributes () -> href