开发者

php simplexml get maximum value

开发者 https://www.devze.com 2023-03-21 19:15 出处:网络
How is it possible to get the highest existing guid value from an existing xml file (SimpleXml). xml-structure example:

How is it possible to get the highest existing guid value from an existing xml file (SimpleXml).

xml-structure example:

<xml blabla>
 <blog name="">
  <post>
   <guid>3</guid>
   <author></author>
   <content>...</content>
  </post>
  <post>
   <guid>1</guid>
   <author></author>
   <content>...</conte开发者_开发百科nt>
  </post>
 </blog>

I tried the following ($xml is an SimpleXml Object) max($xml->xpath(/blog/post/guid)); but this seems to be no array...

any suggestions? Is there an way to handle it per xpath? My google search wasn't successful.


You could use array_map('intval'... to feed max() with something it "understands".

<?php
$xml = getDoc();
$ns = $xml->xpath('//blog/post/guid');
$ns = array_map('intval', $ns);
echo max($ns);

function getDoc() {
    return new SimpleXMLElement( <<< eox
<xml>
 <blog name="">
  <post>
   <guid>3</guid>
   <author></author>
   <content>...</content>
  </post>
  <post>
   <guid>1</guid>
   <author></author>
   <content>...</content>
  </post>
  <post>
   <guid>99</guid>
   <author></author>
   <content>...</content>
  </post>
  <post>
   <guid>47</guid>
   <author></author>
   <content>...</content>
  </post>
 </blog>
</xml>
eox
    );
}
0

精彩评论

暂无评论...
验证码 换一张
取 消