Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multisort on 2 fields

Status
Not open for further replies.

KryptoS

Programmer
Feb 7, 2001
240
0
0
BE
Hello,

I found a solution for this, but lost it and don't know how I did it. So maybe someone could help me..

array[0]['company'] = 'Company 1';
array[0]['name'] = 'Name 1';
array[1]['company'] = 'Company 1';
array[1]['name'] = 'Name 2';

that's how my array looks like. Now I want to sort it first on the key 'company' and in this sort on the key 'name'.

thank you.

visit my website at
 
Hi

Like this ?
Code:
[url=http://php.net/usort/]usort[/url]($array,'hmm');

function hmm($a,$b)
{
  if ($a['company']!=$b['company']) return $a['company']>$b['company']?-1:1;
  else return $a['name']>$b['name']?-1:1;
}

Feherke.
 
Sorry, I just found the solution. (why do we always find the solution after posting?)

foreach($array as $key=>$var) {
$company[$key]= $var['company'];
$name[$key] = $var[name];
}
array_multisort($company,SORT_ASC,$name,SORT_ASC,$array);

visit my website at
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top