Sort XML via attribute value PHP

后端 未结 6 810
忘掉有多难
忘掉有多难 2020-12-01 20:25

So I have an XML file that I am trying to loop through in order, according to the attribute, \"order\".

Here is an example:




        
6条回答
  •  孤街浪徒
    2020-12-01 20:54

    For future reference, here's something you can use to query nodes via XPath and sort the result via XPath as well: SimpleDOM. In this example, I sort all nodes by values of the order attribute:

    include 'SimpleDOM.php';
    
    $page = simpledom_load_string('
        
            
            
            
        
    ');
    
    $nodes = $page->sortedXPath('//tree', '@order');
    
    foreach ($nodes as $node)
    {
        echo $node->asXML(), "\n";
    }
    

提交回复
热议问题