How to sort a multi-dimensional XML file?

前端 未结 1 709
旧巷少年郎
旧巷少年郎 2020-12-07 04:50

I have tried to get an XML file to sort and have had no luck. After a day and a-half, I need some help from an expert. Thanks.

My XML File (shortened for the example

1条回答
  •  Happy的楠姐
    2020-12-07 05:37

    Okay, sorry for going around the houses before - I've added a different answer for clarity but using the sort proxying technique I linked to.

    function xsort(&$nodes, $child_name, $order=SORT_ASC)
    {
        $sort_proxy = array();
    
        foreach ($nodes as $k => $node) {
            $sort_proxy[$k] = (string) $node->$child_name;
        }
    
        array_multisort($sort_proxy, $order, $nodes);
    }
    
    $structure = '
    
        
            2010-06-01
            Application for Summer Due
        
        
            2010-07-01
            Application for Fall Due
        
        
            2010-07-31
            Summer Bill Due
        
    ';
    
    $xml = simplexml_load_string($structure);
    $nodes = $xml->xpath('/deadlines/deadline');
    
    // Sort by date, descending
    xsort($nodes, 'date', SORT_DESC);
    var_dump($nodes);
    

    0 讨论(0)
提交回复
热议问题