3

可能重复:
SimpleXMLElement 对象

嗨,我想对 XML 文档进行排序 - 我这样做是这样的:

public 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);

}

我得到了$nodes变量中的排序数组。在代码中它以这种方式使用:

$this->xsort($nodes, 'kategoria_6', SORT_DESC);

当我做 print_r($nodes); 我得到排序视图

SimpleXMLElement Object (
  [kategoria_6] => kat4 
  [opis_6] => opis4 
  [img] => slides/slide3.jpg
) 
SimpleXMLElement Object ( 
  [kategoria_6] => kat3 
  [opis_6] => opis3 
  [img] => slides/slide2.jpg 
) 
SimpleXMLElement Object ( 
  [kategoria_6] => kat2 
  [opis_6] => opis2 
  [img] => slides/slide1.jpg 
) 
SimpleXMLElement Object ( 
  [kategoria_6] => kat1 
  [opis_6] => opis1 
  [img] => slides/slide0.jpg 
) 
SimpleXMLElement Object ( 
  [kategoria_6] => kat1 
  [opis_6] => opis1 
  [img] => slides/slide4.jpg 
)

但是当我想将它保存到文件中时,它是未排序的。文件保存代码:

$dom = new DOMDocument('1.0', 'utf-8');
foreach ($nodes as $element) {
    $dom = dom_import_simplexml($element)->ownerDocument;
}
$dom->save("c:\\aaa.xml");

有谁知道为什么它保存未排序?

4

0 回答 0