Help me please. How to get the contents of the price tag if the contents of the name tag are known. My attempts:

<?php $title = "item-name" $stocks = simplexml_load_file('http://website.com/upload/stocks.xml'); $stocks = $stocks->xpath('/item/[name=$title]/price'); foreach($stocks as $stock) { echo $stocks->price; } ?> 

XML file:

 <data vendor="SONEX" date="2016-10-28T18:07:45"> <items> <item> <name>2590/2W</name> <price>2675</price> </item> <item> <name>2599</name> <price>53845</price> </item> </items> </data> 

    2 answers 2

     $stocks = simplexml_load_file('http://website.com/upload/stocks.xml'); foreach($stocks->items->item as $stock) { if ($stock->name == "какое-то значение") { echo $stock->price } } 
    • not really. probably should be if ($stock->name == "какое-то значение") echo $stock->price; - Alexey Shimansky
    • @ AlekseyShimansky, if the author cannot use the if construct himself, then there is nothing for him to do in programming :). But for the purity of the answer I will add your note - ArchDemon

    Probably better turn immediately on the structure

     $title = "item-name"; $stocks = simplexml_load_file('http://website.com/upload/stocks.xml'); echo $stocks->items->item[0]->price;