Sort Array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
moiseszaragoza
Forum Commoner
Posts: 87
Joined: Sun Oct 03, 2004 4:04 pm
Location: Ft lauderdale
Contact:

Sort Array

Post by moiseszaragoza »

Ok What i Need to do is sort the array the order

I have

Code: Select all

 
$array_Test['TestID'][1] ="1"
$array_Test['Name'][1] = "Joe"
$array_Test['order'][1] ="3"
 
$array_Test['TestID'][2] ="2"
$array_Test['Name'][2] = "Ana"
$array_Test['order'][2] ="1"
 
$array_Test['TestID'][3] ="3"
$array_Test['Name'][3] = "Mara"
$array_Test['order'][3] ="2"
 
and i need them to end up like

Code: Select all

 
$array_Test['TestID'][1] ="3"
$array_Test['Name'][1] = "Mara"
$array_Test['order'][1] ="2"
 
$array_Test['TestID'][2] ="2"
$array_Test['Name'][2] = "Ana"
$array_Test['order'][2] ="1"
 
$array_Test['TestID'][3] ="1"
$array_Test['Name'][3] = "Joe"
$array_Test['order'][3] ="3"
 
 
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Sort Array

Post by mikemike »

User avatar
dheeraj
Forum Commoner
Posts: 40
Joined: Fri Feb 06, 2009 11:54 am

Re: Sort Array

Post by dheeraj »

Code: Select all

$array_Test['TestID'][1] ="1"
$array_Test['Name'][1] = "Joe"
$array_Test['order'][1] ="3"
 
$array_Test['TestID'][2] ="2"
$array_Test['Name'][2] = "Ana"
$array_Test['order'][2] ="1"
 
$array_Test['TestID'][3] ="3"
$array_Test['Name'][3] = "Mara"
$array_Test['order'][3] ="2"
Solution

Code: Select all

 
for($a = 0; $a < 3; $a++)
{
   for($b = $a+1; $b < 3; $b++)
   {
      if($array_Test['TestID'][$a] < $array_Test['TestID'][$b])
      {
         $sort = $array_Test['TestID'][$b];
         $array_Test['TestID'][$b] = $array_Test['TestID'][$a];
         $array_Test['TestID'][$a] = $sort;
      }
   }
}
Not sure tht it will work sorry if it not, just put this code & print ur array...
User avatar
moiseszaragoza
Forum Commoner
Posts: 87
Joined: Sun Oct 03, 2004 4:04 pm
Location: Ft lauderdale
Contact:

Re: Sort Array

Post by moiseszaragoza »

Thanks.
I am not lookog to reverse my array, but to sort it by the order field
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Sort Array

Post by Mark Baker »

It would be a easier if you worked your array slightly differently:

Code: Select all

 
$array_Test[1]['TestID'] ="1";
$array_Test[1]['Name'] = "Joe";
$array_Test[1]['order'] ="3";
 
$array_Test[2]['TestID'] ="2";
$array_Test[2]['Name'] = "Ana";
$array_Test[2]['order'] ="1";
 
$array_Test[3]['TestID'] ="3";
$array_Test[3]['Name'] = "Mara";
$array_Test[3]['order'] ="2";
 
 
echo '<pre>';
print_r($array_Test);
echo '</pre>';
 
function orderSort($a, $b) {
    if ($a['order'] == $b['order']) {
        return 0;
    }
    return ($a['order'] < $b['order']) ? -1 : 1;
}
 
usort($array_Test,'orderSort');
 
echo '<hr /><pre>';
print_r($array_Test);
echo '</pre>';
 
but try

Code: Select all

array_multisort($array_Test['order'], SORT_ASC, $array_Test);
User avatar
moiseszaragoza
Forum Commoner
Posts: 87
Joined: Sun Oct 03, 2004 4:04 pm
Location: Ft lauderdale
Contact:

Re: Sort Array

Post by moiseszaragoza »

Thanks i am trying it out
how do i get my new content out?
i am gettting a error that reads
Warning: array_multisort() [function.array-multisort]: Array sizes are inconsistent in /home/content/m/p/m/mpmiami2/html/admin/secure/index_.php on line 79
This is what i have

Code: Select all

 
 <?php
                            $array_OriginalOrder = array();
                            $i=0;
                            while ($i <= $count_Team-1):
                                // GET TEAM 
                                $TeamID = $array_Team['TeamID'][$i];
                                // Get Total 
                                getAllTOTALByTeam($TeamID);
                                // PopulateAttay
                                $array_OriginalOrder['TeamID'][$i]                  = $TeamID;
                                $array_OriginalOrder['count_getAllTOTALByTeam'][$i] = $count_getAllTOTALByTeam;
                                
                                echo($array_OriginalOrder['TeamID'][$i]);
                                echo(" "); 
                                echo($array_OriginalOrder['count_getAllTOTALByTeam'][$i]);
                                echo("<br /> "); 
                            $i++;
                            endwhile;
                                echo(" <br /> "); 
                            array_multisort($array_OriginalOrder['count_getAllTOTALByTeam'], SORT_ASC, $array_OriginalOrder);
                                echo("<br /> "); 
                            $i=0;
                            while ($i <= $count_Team-1):
                                echo($array_OriginalOrder['TeamID'][$i]);
                                echo(" "); 
                                echo($array_OriginalOrder['count_getAllTOTALByTeam'][$i]);
                                echo("<br /> "); 
                            $i++;
                            endwhile;
                        ?>
 
Out Put
1 4
2 0
3 1
4 0
5 16
6 5
7 0
8 2
9 0
10 0
11 0
12 0
13 0
14 8
15 0
16 5


Warning: array_multisort() [function.array-multisort]: Array sizes are inconsistent in /home/content/m/p/m/mpmiami2/html/admin/secure/index_.php on line 79

1 4
2 0
3 1
4 0
5 16
6 5
7 0
8 2
9 0
10 0
11 0
12 0
13 0
14 8
15 0
16 5
Post Reply