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!