php xpath sort alphabetically by field

﹥>﹥吖頭↗ 提交于 2019-12-24 20:58:51

问题


I'd like to have a more accurate alphabetical sort for xpath. My current system only sorts the first two characters of the field... I'd like it the order the whole field if possible.

foreach(range('A','Z') AS $firstletter) {
     foreach(range('a','z') AS $secondletter) {
              $letters = $firstletter.$secondletter;
              if($item->xpath("/Entries
                                  /Entry[
                                     starts-with(
                                        Field42,
                                        '".$letters."'
                                     )
                                   and 
                                     Field380 = 'Okay'
                                  ]")) {

The Field42 entries are lastnames (ie Brown, Brownstein, Brownwood, Byrnes,..)


回答1:


I don't think you'll be able to sort using XPath (its goal being to select data -- not sort it).

So, why not do it in pure-PHP :

  • First, load all of your nodes to a PHP array
  • And, then, sort that array using one of the functions provided by PHP
    • Such as sort, asort, usort, ...


来源:https://stackoverflow.com/questions/5598595/php-xpath-sort-alphabetically-by-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!