Jump to content

[SOLVED] Sorting an 'array of arrays' by one index?


teamv

Recommended Posts

Hi all,

 

I have a large array filled with other arrays, each of which is slightly different, but they all have an index "time" which is a unix timestamp. Is there any easy function which will sort this arrays of array for me by that time index (highest to lowest), or will I have to use some kind of more complex recursive algorithm?  Would greatly appreciate any advice people can offer me on this question.

 

Thanks in advance for any and all suggestions.

 

Richard

Link to comment
Share on other sites

usort, uksrt, uasort. (check out php.net for usage/examples)

www.php.net/usort - basic sorting function

www.php.net/uasort - maintains index association

www.php.net/uksort - sort based on keys

 

There will take your array and a user-defined function and sort your multi-dim. arrays

 

typical user defined functions might be something like:

function mySort1 ($x, $y) {
    return ($x['key1'] > $y['Key1']);
}

 

and you implement it like:

usort($array, 'mySort1');//usort provides the function arguments

Link to comment
Share on other sites

No.

 

I'm saying your mysort() function is wrong and should be like GingerRobots

 

Oh, well I did try theirs first but it was sorted in the opposite order than I wanted. I then tried changing the - to a + sign as I expected that to switch it round, but it didn't. What I am missing?

 

Thanks for all your help support thus far!

Link to comment
Share on other sites

Ah that makes a lot more sense than a plus sign :-[, I should have tried to think through the logic of what was happening in the function rather than just making a change like I did, sorry. I have now made the changes given above though and all is working well!

 

Well thanks for sticking with me guys, and for all your help. You've been great!

 

Richard

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.