I Have This Xml File <?xml version="1.0" encoding="UTF-8"?> <library> <book id="1"> <title>PHP and MySQL</title> <author id="test">Miguel Alvarez</author> <publisher>WebHole</publisher> <price>1.99</price> </book> <book id="2"> <title>JAVA 123</title> <author id="test2">WIlliam Vega</author> <publisher>WebHole</publisher> <price>2.99</price> </book> </library> Code (markup): And This PHP file <?php $url = "test.xml"; $xml = simplexml_load_file($url); foreach ($xml->book as $entry){ echo $entry->author . ' <br />'; } ?> PHP: It'll show Miguel Alvarez WIlliam Vega how can I get only Miguel Alvarez as result, Like foreach ($xml->book['id=1'] as $entry) Or echo $entry->author['id=test'] . ' <br />'; Please help me
You probably want to look at using SimpleXMLElement::xpath to accomplish that. http://php.net/manual/en/simplexmlelement.xpath.php Or you could always just stick some if() logic inside your foreach().