Page 1 of 1
Sort Array
Posted: Sun May 31, 2009 9:07 pm
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"
Re: Sort Array
Posted: Sun May 31, 2009 9:56 pm
by mikemike
Re: Sort Array
Posted: Mon Jun 01, 2009 9:31 am
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...
Re: Sort Array
Posted: Mon Jun 01, 2009 10:16 am
by moiseszaragoza
Thanks.
I am not lookog to reverse my array, but to sort it by the order field
Re: Sort Array
Posted: Mon Jun 01, 2009 11:15 am
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);
Re: Sort Array
Posted: Mon Jun 01, 2009 2:07 pm
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