Ok, first of all, I'm not even sure the title is right, if so, I'm sorry.
I have this loop here which is the result of a MongoDB query:
foreach($cursor as $obj) {
$monster = $obj["type"];
$strenght = $obj["strenght"];
$obj["value"] = rand(5, 15);
}
now, I have put rand
there to signify that value
changes for each iteration. Now i want that this array, when is printed, is ordered by that $obj["value"]
, and be able to chose if ascending or descending.
ok, I have tried this
foreach($cursor as $obj) {
$type = $obj["monster"];
$strenght = $obj["strenght"];
$obj["value"] = rand(5, 15);
$newarr[] = $obj;
}
usort($newarr, "cmp");
function cmp($a, $b)
{ return $b['value'] < $a['value']; }
foreach ($newarr as $obj)
{
echo $obj['value'] . $obj['type'] . "<br/>";
}
As I expected, the
$obj["value"] = rand(5, 15);
does not get lost at every iteration in fact, the $newarr contains that value, the problem is that it does not sort them at all. The items are printed in the same order as they were put inside the array. Any help?
Thanks