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
https://forums.phpfreaks.com/topic/62250-solved-sorting-two-dimensional-array/
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

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; 
}
?>

  • 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.

You can't use the $data array like that. mysql_fetch_array() gets a row of data from a mysql result set. try

 

<?php
$urank=0;
foreach ($data as $user)
{
$urank++;
if ($urank != $user['rank'])
	dbquery("UPDATE $playerdb SET rank=$urank WHERE num={$user['num']}");

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.