multidim array sort

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I am wondering if someone can point me to the needed sort function. There are so many of  
them that I simply got lost by reading their descriptions. I try to explain what I need.

I have a multidimensional array and I have a function that outputs it as a table.

$arr[0] contains an array that contains usernames
$arr[1] contains an array that contains real names
$arr[2] contains an array that contains e-mails
$arr[last] contains integers for users "levels"

My function outputs users with their datas in the columns. Each column means one user.

Now I want to sort this whole array by levels. So, all other data should be sorted  
according to the pattern in levels row.

How is this thing done?

Please advice!


Re: multidim array sort

Abhi wrote:

Quoted text here. Click to load it

Hi Abhi,

You need array_multisort.

Here follows a basic example:

  $arr["username"] = array("John" , "Bert" , "Theo" , "Peter");
  $arr["realname"] = array("Johnny Bravo" , "Bert Visser" , "Theo Maasen" ,  
"Peter Hammil");
  $arr["email"] = array("" , "" ,  
"" , "");
  $arr["level"] = array( 8 , 5 , 3 , 5);
 <? print_r($arr); ?>
  // sort
  array_multisort($arr["level"] , $arr["username"] , $arr["realname"] ,  
 <? print_r($arr); ?>


When using array_multisort remember:
1) Your original arrays (the ones you feed to the function) will change  
order, so no 'resultarray'. The function just return true on succes and  
false on failure.
2) The ordering will be done in order of assigned arrays.
Or to put it more easily: look at the passed arrays as COLUMNS for a table.

Read it all at

Erwin Moller

Site Timeline