1

How can I directly call id="url1" without a foreach?

Like this:

echo $item->url1; // Output: http://url1.com

This is code I have:

<?php
$string = <<<XML
<url>
    <item id="url1">http://url1.com/</item>
    <item id="url2">http://url2.com/</item>
</url>
XML;

$xml = simplexml_load_string($string);


foreach($xml->item AS $key => $value){
    echo $value['id'].' = "'.$value.'"<br />';
}
?>

This is the output from the code I have:

url1 = "http://url1.com/"
url2 = "http://url2.com/"
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
rraayy
  • 119
  • 8
  • This is easy with XPath. Try searching for questions mentioning that and SimpleXML, and you should find some examples of how to use it. – IMSoP Dec 19 '14 at 13:01

1 Answers1

1

You can query the document using XPath syntax:

$value = $rules->xpath('item[@id="url1"]')[0];
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309