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");
0

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

4 Comments

Its working for me @kupendra Check Demo it results as you have shown within your expected output
Are you getting any errors or else or post your original array
Are you sure that you have placed code as shown within example or demo.
Update your question what you have tried with the relevant code
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.