xmlデータは次のようになります。
<feed>
<entry>
<id>12345</id>
<title>Lorem ipsum</title>
<link type="type1" href="https://foo.bar" />
<link type="type2" href="https://foo2.bar"/>
</entry>
<entry>
<id>56789</id>
<title>ipsum</title>
<link type="type2" href="https://foo4.bar"/>
<link type="type1" href="https://foo3.bar" />
</entry>
</feed>
特定のタイプのリンクからhref属性のコンテンツを選択したい。(タイプ1が常に最初のリンクであるとは限らないことに注意してください)
動作するコードの一部:
for($i=0; $i<=5; $i++) {
foreach($xml->entry[$i]->link as $a) {
if($a["type"] == "type2")
$link = (string)($a["href"]);
}
}
ただし、foreachループを必要としない、より高速で洗練されたソリューションがあるのではないかと思います。何か案は?