Sorting a multiarray

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
Jabica
Forum Newbie
Posts: 3
Joined: Fri Aug 22, 2008 10:31 pm

Sorting a multiarray

Post 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.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Sorting a multiarray

Post by jaoudestudios »

If it is pulled from a database why not just sort it in the database? That is what it is designed for! :)
Jabica
Forum Newbie
Posts: 3
Joined: Fri Aug 22, 2008 10:31 pm

Re: Sorting a multiarray

Post 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
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Sorting a multiarray

Post by jaoudestudios »

You can still do percentages in sql. Look into it it might make your life easier. Just a thought!
Jabica
Forum Newbie
Posts: 3
Joined: Fri Aug 22, 2008 10:31 pm

Re: Sorting a multiarray

Post 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.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Sorting a multiarray

Post 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.
Post Reply