sorting a multi-dim array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
toms100
Forum Contributor
Posts: 119
Joined: Wed Feb 26, 2003 10:29 am
Location: Bristol,UK

sorting a multi-dim array

Post by toms100 »

ok
i have an array, $planet
it works like this
$planet[Planetnumber][someinfo] = statistic
planet number is any number
some info can be a number 0-7, with each number corresponding to a diferent value
for the purpose of my question, i am looking at 5, which is score.
now i have successfully filled the array with data but i am looking to order the array to display the items in order of score (Desc), but i cant work out what to do.

Code: Select all

array_multisort($planet[*][5], SORT_DESC, SORT_NUMERIC);
that returns an error...
what should i be using and what sintax?
i hope someone can help

many thanks

Tom
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

sounds more like a usort() to me.

Code: Select all

function descendingByScore($planetA, $planetB)
{
	if ($planetA[5] > $planetB[5])
		return -1;
	elseif ($planetA[5] == $planetB[5])
		return 0;
	else
		return 1;
}

...

usort($planet, 'descendingByScore');
toms100
Forum Contributor
Posts: 119
Joined: Wed Feb 26, 2003 10:29 am
Location: Bristol,UK

Post by toms100 »

Yey thank you very much.
very usefull post, worked a treat:)

Tom
Post Reply