1

I have following array :

$inboxMessages = array(
    105775 => array( //index is thread_id
        0 => array(
            'id' => 85,
            'thread_id' => 105775,
            'message' => "hello",
            'created_at' => "May 20, 2015",
            'datetime' => 1432118191,
            'sender_id' => 13,
        ),
        1 => array(
            'id' => 70,
            'thread_id' => 105775,
            'message' => "hii",
            'created_at' => "May 19, 2015",
            'datetime' => 1432021227,
            'sender_id' => 13,
        )
    ),
    224199 => array( //index is thread_id
        0 => array(
            'id' => 88,
            'thread_id' => 224199,
            'message' => "yessss...",
            'created_at' => "May 20, 2015",
            'datetime' => 1432306513,
            'sender_id' => 14,
        ),
        1 => array( //index is thread_id
            'id' => 75,
            'thread_id' => 224199,
            'message' => "hellowwww...",
            'created_at' => "May 19, 2015",
            'datetime' => 1432021227,
            'sender_id' => 14,
        )
    ),
    107917 => array( //index is thread_id
        0 => array(
            'id' => 56,
            'thread_id' => 107917,
            'message' => "how r u??",
            'created_at' => "May 16, 2015",
            'datetime' => 1431792155,
            'sender_id' => 14,
        ),
        1 => array(
            'id' => 30,
            'thread_id' => 107917,
            'message' => "hi.. i m fine.",
            'created_at' => "May 6, 2015",
            'datetime' => 1430920006,
            'sender_id' => 14,
        ),
        2 => array(
            'id' => 30,
            'thread_id' => 107917,
            'message' => "hi!!!!..how r u??",
            'created_at' => "May 6, 2015",
            'datetime' => 1430920006,
            'sender_id' => 14,
        )
    ),
    378552 => array( //index is thread_id
        0 => array(
            'id' => 108,
            'thread_id' => 378552,
            'message' => "hi",
            'created_at' => "May 29, 2015",
            'datetime' => 1432906923,
            'sender_id' => 14,
        ),
        1 => array(
            'id' => 107,
            'thread_id' => 378552,
            'message' => "hi.",
            'created_at' => "May 29, 2015",
            'datetime' => 1432903194,
            'sender_id' => 14,
        )
    )
);

So i need Output like this :

$inboxMessages = array(
    378552 => array( //index is thread_id
        0 => array(
            'id' => 108,
            'thread_id' => 378552,
            'message' => "hi",
            'created_at' => "May 29, 2015",
            'datetime' => 1432906923,
            'sender_id' => 14,
        ),
        1 => array(
            'id' => 107,
            'thread_id' => 378552,
            'message' => "hi.",
            'created_at' => "May 29, 2015",
            'datetime' => 1432903194,
            'sender_id' => 14,
        )
    ),
    224199 => array( //index is thread_id
        0 => array(
            'id' => 88,
            'thread_id' => 224199,
            'message' => "yessss...",
            'created_at' => "May 20, 2015",
            'datetime' => 1432306513,
            'sender_id' => 14,
        ),
        1 => array( //index is thread_id
            'id' => 75,
            'thread_id' => 224199,
            'message' => "hellowwww...",
            'created_at' => "May 19, 2015",
            'datetime' => 1432021227,
            'sender_id' => 14,
        )
    ),
    105775 => array( //index is thread_id
        0 => array(
            'id' => 85,
            'thread_id' => 105775,
            'message' => "hello",
            'created_at' => "May 20, 2015",
            'datetime' => 1432118191,
            'sender_id' => 13,
        ),
        1 => array(
            'id' => 70,
            'thread_id' => 105775,
            'message' => "hii",
            'created_at' => "May 19, 2015",
            'datetime' => 1432021227,
            'sender_id' => 13,
        )
    ),
    107917 => array( //index is thread_id
        0 => array(
            'id' => 56,
            'thread_id' => 107917,
            'message' => "how r u??",
            'created_at' => "May 16, 2015",
            'datetime' => 1431792155,
            'sender_id' => 14,
        ),
        1 => array(
            'id' => 30,
            'thread_id' => 107917,
            'message' => "hi.. i m fine.",
            'created_at' => "May 6, 2015",
            'datetime' => 1430920006,
            'sender_id' => 14,
        ),
        2 => array(
            'id' => 30,
            'thread_id' => 107917,
            'message' => "hi!!!!..how r u??",
            'created_at' => "May 6, 2015",
            'datetime' => 1430920006,
            'sender_id' => 14,
        )
    ),

);

I want to sort main array by value of datetime sub array of thread_id.

In array of thread_id is already sorting.

So i just want to sort main array by thread_id's sub array.

I need recent datetime is top on array

I tried so far And its works for me:

function comp($a, $b) {
    if ($a[0]->datetime == $b[0]->datetime) {
        return 0;
    } 
    return $a[0]->datetime > $b[0]->datetime ? -1 : 1;
}

uasort($inboxMessages, "comp");

And also try with message id :

function comp($a, $b) {
    if ($a[0]->id == $b[0]->id) {
        return 0;
    } 
    return $a[0]->id > $b[0]->id ? -1 : 1;
}

uasort($inboxMessages, "comp");
kupendra
  • 1,002
  • 1
  • 15
  • 37

2 Answers2

1

Use uasort

function cmp($a, $b) {
    if ($a[0]['datetime'] == $b[0]['datetime']) {
        return 0;
    }
    return ($a[0]['datetime'] < $b[0]['datetime']) ? 1 : -1;
}
uasort($inboxMessages, "cmp");

Depending on PHP version, you can specify "cmp" as an anonymous function. Also keep in mind this method relies on the fact that each thread has at least one message in it.

vakata
  • 3,726
  • 1
  • 17
  • 31
1

You can use uasort() as follows to achieve the following format

function comp($a, $b) {
    if ($a[0]['datetime'] == $b[0]['datetime']) {
        return 0;
    } return $a[0]['datetime'] > $b[0]['datetime'] ? -1 : 1;
}
uasort($inboxMessages, "comp");

Check Demo

Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54