当它来到php和xml但是如何从xml文件中获取最新的3个项目时有点像菜鸟?
$xmlstr= '<?xml version="1.0"?>
<doc>
<tran>
<balance>25000</balance>
<amount>560</amount>
</tran>
<tran>
<amount>5999</amount>
<balance>30999</balance>
</tran>
<tran>
<amount>5000</amount>
<balance>20000</balance>
</tran>
<tran>
<amount>8923</amount>
<balance>25000</balance>
</tran>
...
...
</doc>';
答案 0 :(得分:1)
你可以use XPath with SimpleXML。以下XPath应返回XML中的最后3个tran
元素:
$xml = simplexml_load_string($xmlstr);
$result = $xml->xpath("//tran[position() > last()-3]");