Jump to content

[SOLVED] sorting two-dimensional array


mdnghtblue

Recommended Posts

Still kind of a newbie to this stuff....I've been trying to figure this out for a while.

 

This is what I want to do:

 

1. pull a query from the database, "ORDER BY disabled ASC, networth DESC"

2. check every user's info from the query, to see if they have a certain "ability" enabled

3. if they do, perform a calculation on networth, and put it back into the array/query (not sure what to call it at this point)

4. reorder the array/query by "disabled ascending, networth descending"

 

This has been driving me nuts, I feel like it's got to be possible.

 

I'm running into a couple problems, which is due to lack of complete knowledge of how PHP works. =/ First, the "calculations" I'm performing on those certain users aren't being stored, and I'm pretty sure the sorting isn't working either. I tried to use array_multisort, but I'm not sure it's working.

 

$users = dbquery("SELECT disabled, networth, num, rank, ability, abilitytime FROM $playerdb ORDER BY disabled ASC, networth DESC;");

mysql_data_seek($users, 0);
while($user = mysql_fetch_assoc($users))
{
   if($user[ability] == 50)
   {
   	$fakenw = $user[networth];
   	for($i=0; $i < $user[abilitytime]; $i++)
   	     $fakenw -= ($fakenw * 0.01);
   	$user[networth] = round($fakenw,0);
   }
}
mysql_data_seek($users, 0);
$user = mysql_fetch_assoc($users);
foreach($user as $key => $row)
{
$disabled[$key] = $row['disabled'];
$networth[$key] = $row['networth'];
}
array_multisort($disabled, SORT_ASC, $networth, SORT_DESC, $user);

 

I don't think I have any idea what I'm doing. =(

Link to comment
Share on other sites

ok, withouth going through your code, i hope hte below code works.

 

$Query = QUERY STUFF;

$New_Array = array();

 

if($QUERY = "TEST") {

        $New_Array[''][''] = ""; # rearranging your indexes, or simply adding the content

}

 

then there is sort($New_Array, ASC) or something, but im not sure about eh actual arguments

 

gdlk

Link to comment
Share on other sites

try

<?php

$users = dbquery("SELECT disabled, networth, num, rank, ability, abilitytime FROM $playerdb ORDER BY disabled ASC, networth DESC;");

$data = array();
while($user = mysql_fetch_assoc($users))
{
    if($user['ability'] == 50)
    {
        $fakenw = $user['networth'];
        for($i=0; $i < $user['abilitytime']; $i++)
   	         $fakenw -= ($fakenw * 0.01);
        $user['networth'] = round($fakenw,0);
    }
    /**
    * put data in array
    */
    $data[] = $user;
    
}

/**
* sort the data using custom mysort() function
*/
usort ($data, 'mysort');

/**
* view the sorted array
*/
echo '<pre>', print_r($data, true), '</pre>';

/**
* mysort function
*    if a should sort above b, return -1
*    if a and be are cosidered equal, return 0
*    if a should sort below b, return 1
*/
function mysort($a, $b)
{
    if ($a['disabled'] == $b['disabled'])
    {
        // sort networth DESC
        if ($a['networth'] == $b['networth']) return 0;
        return $a['networth'] > $b['networth'] ? -1 : 1;
    }
    return $a['disabled'] < $b['disabled'] ? -1 : 1; 
}
?>

Link to comment
Share on other sites

  • 3 weeks later...

Tried it, but it didn't work. After the array has been sorted, this code is executed:

 

while ($user = mysql_fetch_array($data))
{
$urank++;
if ($urank != $user[rank])
	dbquery("UPDATE $playerdb SET rank=$urank WHERE num=$user[num];");
}

 

I think the sorting worked though.

Link to comment
Share on other sites

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.