Php 具有2个href属性的XML数组
我有以下XML数组:Php 具有2个href属性的XML数组,php,Php,我有以下XML数组: ["link"]=> array(2) { [0]=> object(SimpleXMLElement)#311 (1) { ["@attributes"]=> array(3) { ["type"]=> string(9) "text/html"
["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作为链接,另一个带有一个链接
如何将每个href输出,而不是将两者一起输出
目前正在尝试:
foreach($entry->link as $link) {
echo $link->attributes()->href;
}
您可以使用普通数组/对象访问访问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的方法
我认为您不能直接访问属性$elment->@attributes,因为这不是有效的语法。First href-$xml['link'][0]->attributes->href
$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;
}
第二个href-$xml['link'][1]->属性->href
$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>
----
但您可能也会感兴趣在显示的循环中,从回声中得到的输出是什么?您是否可以再粘贴一些代码,因为不清楚当前有哪些循环。谢谢。我得到了一个字符串,比如:concatonatedtogether如果您从$xml->asxml而不是var_dump$xml;发布输出,那么会更容易;。或者说你正在通过simplexml解析提要,因为这就是你正在做的,不是吗?是的,我只是想发布所有内容。