hi

this will do half of what you want. i found it in http://nz2.php.net/sort.

 

function sortByField($multArray,$sortField,$desc=true)
{
$tmpKey='';
$ResArray=array();

$maIndex=array_keys($multArray);

$maSize=count($multArray)-1;

for($i=0; $i < $maSize ; $i++) 
{
$minElement=$i;

$tempMin=$multArray[$maIndex[$i]][$sortField];

$tmpKey=$maIndex[$i];

for($j=$i+1; $j <= $maSize; $j++)
{
if($multArray[$maIndex[$j]][$sortField] < $tempMin ) 
{
$minElement=$j;
$tmpKey=$maIndex[$j];
$tempMin=$multArray[$maIndex[$j]][$sortField];
}
}
$maIndex[$minElement]=$maIndex[$i];
$maIndex[$i]=$tmpKey;
}

if($desc)
{
for($j=0;$j<=$maSize;$j++)
{
$ResArray[$maIndex[$j]]=$multArray[$maIndex[$j]];
}
}
else
{
for($j=$maSize;$j>=0;$j--)
{
$ResArray[$maIndex[$j]]=$multArray[$maIndex[$j]];
}
}

return $ResArray;
}



Thanks

Mohammed Alsharaf

http://www.safitech.com

 


From: [email protected]
To: [email protected]
Subject: [phpug] Re: Sorting Multidimensional Array on values of multiple keys
Date: Tue, 28 Jul 2009 09:10:34 +1200





I know in vb6 I used to load something like this into an AODB recordset and use 
the sort and filtering functions in that to manipulate complex data sets, 
haven’t tried multidimensional sorts in PHP.
I was sure php had multidimensional sort (which might be you usort) does 
ezcomponents or Zend have anything.
 

Andrew 
 


From: [email protected] [mailto:[email protected]] On Behalf Of 
Aaron Cooper
Sent: Tuesday, 28 July 2009 9:00 a.m.
To: [email protected]
Subject: [phpug] Sorting Multidimensional Array on values of multiple keys
 

Morning,

 

A challenge.

 

So I have this array, simple example as follows:

 

$testAry = array(
  array('a' => 1, 'b' => 2, 'c' => 3),
  array('a' => 2, 'b' => 1, 'c' => 3),
  array('a' => 3, 'b' => 2, 'c' => 1),
  array('a' => 1, 'b' => 3, 'c' => 2),
  array('a' => 2, 'b' => 3, 'c' => 1),
  array('a' => 3, 'b' => 1, 'c' => 2)
); 

 

And I want to sort this array as in the same fashion as "ORDER BY" in sql 
statements. So think of the array as a table with columns. In my particular 
usage case, I would like order by 'a' primarily and 'b' secondarily.

 

I imagine the function call will look something like this: 
orderBy($results_array, 'a ASC, b DESC');

 

Does anyone have a function to do this? I found a wrapper under usort() docs 
but it doesn't seem to work, even with it's own test data.

 

And no, I can't do this at DB level unfortunately. The array in question is 
built in various stages.

 

Cheers

Aaron



__________ Information from ESET NOD32 Antivirus, version of virus signature 
database 4282 (20090727) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 

__________ Information from ESET NOD32 Antivirus, version of virus signature 
database 4282 (20090727) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com



_________________________________________________________________
Share your memories online with anyone you want.
http://www.microsoft.com/nz/windows/windowslive/products/photos-share.aspx?tab=1
--~--~---------~--~----~------------~-------~--~----~
NZ PHP Users Group: http://groups.google.com/group/nzphpug
To post, send email to [email protected]
To unsubscribe, send email to
[email protected]
-~----------~----~----~----~------~----~------~--~---

Reply via email to