Jump to content

Recommended Posts

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

    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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.