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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array_multisort problem, not sorting correctly

Status
Not open for further replies.

NateUNI

MIS
Jan 3, 2002
132
US
I'm running into the following problem when I try to use the array_multisort fuction. I'm trying to sort by date [1], then report name [0]. If I print out the multi-dimensional array before the array_multisort function, I recieve the following:

Array ( [0] => Array ( [0] => change [1] => 2003-09-24 [2] => 2003-09-24_change.pdf )
[1] => Array ( [0] => stats_client_summary [1] => 2003-10-06 [2] => 2003-10-06_stats_client_summary.pdf )
[2] => Array ( [0] => change [1] => 2003-10-07 [2] => 2003-10-07_change.pdf ) )

Then I run this statement to sort the multi-dimensional array:

array_multisort($TestArray[1], $TestArray[0]);

This is the sorted array that is returned:

Array ( [0] => Array ( [0] => 2003-09-24 [1] => 2003-09-24_change.pdf [2] => change )
[1] => Array ( [0] => 2003-10-06 [1] => 2003-10-06_stats_client_summary.pdf [2] => stats_client_summary )
[2] => Array ( [0] => change [1] => 2003-10-07 [2] => 2003-10-07_change.pdf ) )

The returned array is screwed up, [1] is moved to [0] in the first 2 rows, and it is not sorted. Does anyone know what I am doing wrong here?? Thanks!!
 
You're invoking the wrong function. array_multisort() sorts the sub-arrays of each element of your outer array, which does not sound like what you want.

If you are looking to sort the outer array by one or more values in the inner sub-arrays, I recommend you look at usort() ( which will sort an array based on a user-defined comparison function.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top