Page 1 of 1

Sorting a multiarray

Posted: Fri Aug 22, 2008 10:39 pm
by Jabica
This is supposed to be a simple thing turned complicated. I got a multi array:

$members_list[key, array]

ie:

$members_list[0, ['a' => 1; 'b' => 'name']]
$members_list[88, ['a' => 1; 'b' => 'name']]
$members_list[25, ['a' => 1; 'b' => 'name']]
$members_list[0, ['a' => 1; 'b' => 'name']]

All i'm trying to do it organize the $members_list by the key using ksort.

ksort($members_list);

But it doesn't. for some reason it just doesn't sort anything at all. The data is pulled from a mysql db, and put into the array which is $members_list's value.

IF is key($members_list) it ill turn up to be the 0,88,25,0,... so why isn't it sorting.

I first thought it to be a string so i intval() to make sure. Then i even tried adding that key to the ['sort' => 0, 'a' => 1, 'b' => 'name'] but the result is the same: nothing.

I'm using each members calculated % so i have mixed numbers. Just wondering what was your thoughts on this. I'm sure i'm doing something wrong just can't figure it out.

Thanks for your help.

Re: Sorting a multiarray

Posted: Sat Aug 23, 2008 2:28 am
by jaoudestudios
If it is pulled from a database why not just sort it in the database? That is what it is designed for! :)

Re: Sorting a multiarray

Posted: Sat Aug 23, 2008 1:00 pm
by Jabica
Because i'm calculating the percentages outside with the data from the db those are not pulled from the DB. Should've mentioned that, sorry. But that would be too easy :D

Re: Sorting a multiarray

Posted: Sat Aug 23, 2008 2:32 pm
by jaoudestudios
You can still do percentages in sql. Look into it it might make your life easier. Just a thought!

Re: Sorting a multiarray

Posted: Sat Aug 23, 2008 8:36 pm
by Jabica
Yes but that would mean rewriting a lot of code, i was looking into a simpler solution, since ksort it theory does what i want, i was looking for someone with knowledge into array sorting that could help me fill in the gaps, or shed some light on my problem.

But thanks for your point, it will be an alternative if i can't get anyone to help explain array sorting.

Re: Sorting a multiarray

Posted: Sun Aug 24, 2008 3:34 am
by jaoudestudios
It should not be a lot of code, actually probably less than the amount of php you will need. I appreciate you might be more familiar with php so want to avoid the sql calculation route, but it would probably be much more efficient to do it in sql.

Just a though.

In regards to your array it is sorting it!

Read... http://uk2.php.net/ksort, let me know if dont see the issue.