0

I have the following XML-Document:

<?xml version="1.0" encoding="utf-8"?>
<entries>
  <entry>
    <name>Test</name>
    <ext>2</ext>
  </entry>
  <entry>
    <name>Test 2</name>
    <ext>1</ext>
  </entry>
</entries>

I parse it with

$xml_data = simplexml_load_file("doc.xml");

Then I want to sort the entries (entry-Elements) of the data structure:

function sort($key, $dir, $type)
{
    $srt = create_function('$e1,$e2', '
                          $type = "' . $type . '";
                          $dir  = "' . $dir . '";
                  $key  = "' . $key . '";

                                  if($type == "text")
                           return strcmp($e1->$key, $e2->$key);
                  else if($type == "int")
                  {
                       if((int) $e1->$key == (int) $e2->$key)
                               return 0;
                           else
                               return ((int) $e1->$key < (int) $e2->$key) ? -1 : 1;
                  }
                  else
                  {
                      error_log("Unknown sort type!", 0);
                      return 0;
                                  }
    ');

    usort($xml_data->entry, $srt);
}

An example call looks like:

sort('name', 'ASC', 'text'); // sort order not implemented yet

but it doesnt work because the array seems to be the first entry and not the entry-Array. So I looked what printr says:

printr($xml_data);

says:

SimpleXMLElement Object
(
    [entry] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [name] => Test
                    [ext] => 2
                )

            [1] => SimpleXMLElement Object
                (
                    [name] => Test 2
                    [ext] => 1
                )
        )
)

The problem is: How do I have to call usort.

usort($xml_data->entry, $srt);

seems to be wrong but was my interpretation of the printr-Result.

Can anyone give me a hint or a solution for my problem?

Thank you!

cweiske
  • 30,033
  • 14
  • 133
  • 194
swdn
  • 1
  • 1
  • 1
    Please point out why non of these search results: http://stackoverflow.com/search?q=sort+simplexml+[php] helped solve your question. – Gordon Mar 19 '11 at 13:10
  • 2
    The search results did not give me an answer to my questions because non of those solutions (Maybe I did not find the correct one) modifies the data structure (in my case $xml_data). For example ($nodes = $xml_data->xpath('/entries/entry'); xsort($nodes, $key, SORT_DESC); (http://stackoverflow.com/questions/3998722/how-to-sort-a-multi-dimensional-xml-file) would not solve my problem because I must have the data stored in the given $xml_data variable and not in an new $nodes-Variable. So: I dont want to change the structure. – swdn Mar 19 '11 at 14:03

1 Answers1

0

You cannot sort simplexml elements in an existing instance. You could, however, remove all of them and add them in order afterwards.

cweiske
  • 30,033
  • 14
  • 133
  • 194