Phenomena Posted July 8, 2010 Share Posted July 8, 2010 I'm trying to sort a multidimensional array, but... its not working lol. It's rearranging the array but not in the order I want. I can't even tell what it's being sorted by. >_> My custom strcmp function to compare integers. function cmpi($a, $b) { if ($a["rank"] > $b["rank"]) return 1; elseif ($a["rank"] < $b["rank"]) return -1; else return 0; } building the array: $i=0; foreach($xml_guild_data->guildInfo->guild->members->character as $char) { $member[$i]["name"] = $char["name"]; $member[$i]["level"] = $char["level"]; $member[$i]["rank"] = $char["rank"]; $member[$i]["class"] = $char["classId"]; $i++; } and finally, sorting/generating html table usort($member, 'cmpi'); foreach($member as $mem) { ?> <tr> <td><? echo $mem["level"]; ?></td> <td><? echo $mem["name"]; ?></td> <td><? echo $mem["rank"]; ?></td> </tr> <? } ?> It should be sorting it be the members rank, which is assigned as an integer. Link to comment https://forums.phpfreaks.com/topic/207091-multidimensional-array-sorting/ Share on other sites More sharing options...
jcbones Posted July 8, 2010 Share Posted July 8, 2010 Note: This function assigns new keys to the elements in array. It will remove any existing keys that may have been assigned' date=' rather than just reordering the keys.[/quote'] You may be looking for the function uasort() which preserves array_keys. Link to comment https://forums.phpfreaks.com/topic/207091-multidimensional-array-sorting/#findComment-1082837 Share on other sites More sharing options...
Barand Posted July 8, 2010 Share Posted July 8, 2010 when I ran your cmpi code it worked ok ??? <?php $member = array ( array('name' => 'fred', 'level' => 2, 'rank' => 3, 'class' => 5 ), array('name' => 'john', 'level' => 1, 'rank' => 2, 'class' => 1 ), array('name' => 'henry', 'level' => 1, 'rank' => 4, 'class' => 2 ), array('name' => 'jane', 'level' => 1, 'rank' => 5, 'class' => 5 ), array('name' => 'mary', 'level' => 3, 'rank' => 1, 'class' => 2 ) ); function cmpi($a, $b) { if ($a["rank"] > $b["rank"]) return 1; elseif ($a["rank"] < $b["rank"]) return -1; else return 0; } usort($member, 'cmpi'); foreach ($member as $m) printf ("%d %s<br />", $m['rank'], $m['name']); /* results : 1 mary 2 john 3 fred 4 henry 5 jane **********/ ?> A simpler cmpi function for integer comparisons is function cmpi ($a, $b) { return $a['rank'] - $b['rank]; } Swap the round for a desc sort Link to comment https://forums.phpfreaks.com/topic/207091-multidimensional-array-sorting/#findComment-1082909 Share on other sites More sharing options...
Phenomena Posted July 8, 2010 Author Share Posted July 8, 2010 I found the solution, the rank was being stored as a string instead of an integer. >.< So I just used strcmp() Link to comment https://forums.phpfreaks.com/topic/207091-multidimensional-array-sorting/#findComment-1083248 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.