Jump to content

Recommended Posts

I have an Array and I am attempting to figure out how I can sort it by date.

 

Example of Said Array:

Array
(
    [0] => Array
        (
            [userComments0] => Array
                (
                    [bodyType] => 0
                    [created] => 2011-02-01 23:51:43
                    [currentUserFromGroup] => MEMBER_ONE
                    [body] => We should all wear wooden clogs on Fridays. Who's with me? Huh? Anyone?
                )

        )

    [1] => Array
        (
            [userComments1] => Array
                (
                    [bodyType] => 0
                    [created] => 2011-02-09 18:20:51
                    [currentUserFromGroup] => MEMBER_ONE
                    [body] => Test
                )

        )

    [2] => Array
        (
            [userComments2] => Array
                (
                    [bodyType] => 0
                    [created] => 2011-02-11 21:02:23
                    [currentUserFromGroup] => MEMBER_ONE
                    [body] => TEst
                )

        )

    [3] => Array
        (
            [userComments3] => Array
                (
                    [bodyType] => 0
                    [created] => 2011-02-12 17:02:31
                    [currentUserFromGroup] => MEMBER_ONE
                    [body] => Posting a new idea
                )

        )

    [4] => Array
        (
            [userComments4] => Array
                (
                    [bodyType] => 0
                    [created] => 2011-02-13 23:32:19
                    [currentUserFromGroup] => MEMBER_ONE
                    [body] => Test Idea
                )

        )

    [5] => Array
        (
            [userComments5] => Array
                (
                    [bodyType] => 0
                    [created] => 2011-02-10 01:50:34
                    [currentUserFromGroup] => MEMBER_TWO
                    [body] => Taco sauce is as good as French Toast!
                )

        )

    [6] => Array
        (
            [userComments6] => Array
                (
                    [bodyType] => 0
                    [created] => 2011-02-10 01:51:03
                    [currentUserFromGroup] => MEMBER_TWO
                    [body] => Need to hire more engineers!
                )

        )

    [7] => Array
        (
            [userComments7] => Array
                (
                    [bodyType] => 0
                    [created] => 2011-02-10 01:51:42
                    [currentUserFromGroup] => MEMBER_TWO
                    [body] => Golden Bears are all washed up
                )

        )

    [8] => Array
        (
            [userComments8] => Array
                (
                    [bodyType] => 0
                    [created] => 2011-02-12 00:13:14
                    [currentUserFromGroup] => MEMBER_TWO
                    [body] => The JETS are going to kick ass!
                )

        )

    [9] => Array
        (
            [userComments9] => Array
                (
                    [bodyType] => 0
                    [created] => 2011-02-14 02:23:47
                    [currentUserFromGroup] => MEMBER_TWO
                    [body] => We are getting there!
                )

        )

)

 

But I have stumped myself on it, and cant wrap my mind around getting it sorted by datetime. I don't know where I should begin on this one fellas.

Link to comment
https://forums.phpfreaks.com/topic/228331-sortting-a-multi-dimensional-array/
Share on other sites

Look at php.net's uasort() page. http://us.php.net/manual/en/function.uasort.php. You might have to modify some code to suit your needs, but that function will be useful.

 

Consider this:

 

function compare($x, $y)
{
if ( $x[0]['created'] == $y[0]['created'] )
{
  return 0;
}
return ($a < $b) ? -1 : 1;
}

yes sir, thats what I was working with originally.. My biggest problem was, which I think you resolved for me was having it end up as a 3d arrary rather than 2d. I found myself trying to work around a 2d array. Going back over the way I was creating my array I found that was wrong, after remastering it I got the array type I ultimately wanted, and then from there was able to sort it the way I wanted.. However you answered the question nicely too so credit given to you for that.

Just for reference, what I ended up doing while waiting for an answer here was:

function shiftComments($a ,$b) {
$c = strtotime($a['created']);
$d = strtotime($b['created']);
  if ($c < $d) {
    return -1;
  } elseif  ($c > $d) {
    return 1;
  } else {
    return strcmp($c, $d);
  }
}

 

Only after rebuilding my array to look like:

Array
(
    [0] => Array
        (

                    [bodyType] => 0
                    [created] => 2011-02-01 23:51:43
                    [currentUserFromGroup] => MEMBER_ONE
                    [body] => We should all wear wooden clogs on Fridays. Who's with me? Huh? Anyone?


        )

    [1] => Array
        (

                    [bodyType] => 0
                    [created] => 2011-02-09 18:20:51
                    [currentUserFromGroup] => MEMBER_ONE
                    [body] => Test


        )

    [2] => Array
        (

                    [bodyType] => 0
                    [created] => 2011-02-11 21:02:23
                    [currentUserFromGroup] => MEMBER_ONE


        )

    [3] => Array
        (

                    [bodyType] => 0
                    [created] => 2011-02-12 17:02:31
                    [currentUserFromGroup] => MEMBER_ONE
                    [body] => Posting a new idea


        )

    [4] => Array
        (

                    [bodyType] => 0
                    [created] => 2011-02-13 23:32:19
                    [currentUserFromGroup] => MEMBER_ONE
                    [body] => Test Idea


        )

    [5] => Array
        (

                    [bodyType] => 0
                    [created] => 2011-02-10 01:50:34
                    [currentUserFromGroup] => MEMBER_TWO
                    [body] => Taco sauce is as good as French Toast!


        )

    [6] => Array
        (

                    [bodyType] => 0
                    [created] => 2011-02-10 01:51:03
                    [currentUserFromGroup] => MEMBER_TWO
                    [body] => Need to hire more engineers!


        )

    [7] => Array
        (

                    [bodyType] => 0
                    [created] => 2011-02-10 01:51:42
                    [currentUserFromGroup] => MEMBER_TWO
                    [body] => Golden Bears are all washed up


        )

    [8] => Array
        (

                    [bodyType] => 0
                    [created] => 2011-02-12 00:13:14
                    [currentUserFromGroup] => MEMBER_TWO
                    [body] => The JETS are going to kick ass!

        )

    [9] => Array
        (

                    [bodyType] => 0
                    [created] => 2011-02-14 02:23:47
                    [currentUserFromGroup] => MEMBER_TWO
                    [body] => We are getting there!

        )

 

in comparison to my original posts array

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.