I want to know how can I sort simple XML elements with PHP. Yes, I found some threads that address the same subject — but I couldn\'t solve my problem.
The XML what I wa
There's no real easy way to sort using SimpleXML; you would have to create an array with the elements, sort them and then reconstruct the XML:
$d = simplexml_load_string($xml);
// turn into array
$e = array();
foreach ($d->curso as $curso) {
$e[] = $curso;
}
// sort the array
usort($e, function($a, $b) {
return $a->cargaHoraria - $b->cargaHoraria;
});
// put it back together
echo '<cursos>';
foreach ($e as $node) {
echo $node->saveXML();
}
echo '</cursos>';