I'm having a problem where none of my values are ending up in the right order.
$xml = file_get_contents('admin/people.xml');
$x = new SimpleXMLElement($xml);
$sort=$x->person;
function cmp($a, $b){
if ($a->age == $b->age) {
return 0;
}
return ($a->age < $b->age) ? -1 : 1;
}
usort($sort, 'cmp');
foreach ($sort as $key => $value) {
echo "$key: $value->age<br>";
}
From everything I've read, this should work, but it doesn't. Here is the XML:
<people>
<person>
<name>Frank</name>
<age>12</age>
</person>
<person>
<name>Jim</name>
<age>6023</age>
</person>
<person>
<name>Tony</name>
<age>234</age>
</person>
<person>
<name>Bob</name>
<age>2551</age>
</person>
<person>
<name>Dave</name>
<age>21</age>
</person>
<person>
<name>Trevor</name>
<age>56</age>
</person>
<person>
<name>Mike</name>
<age>89</age>
</person>
</people>
And the result I'm getting is this, which is no kind of order at all!
0: 6023
2: 21
3: 234
4: 12
6: 56
7: 2551
8: 89
Any ideas?
Many thanks...