Page 1 of 1

sorting a multi-dim array

Posted: Fri Jun 20, 2003 3:59 pm
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

Posted: Fri Jun 20, 2003 5:49 pm
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');

Posted: Sat Jun 21, 2003 3:22 am
by toms100
Yey thank you very much.
very usefull post, worked a treat:)

Tom