本文介绍了我要排序的PHP 3维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数组:

  $ inboxMessages =阵列(
    105775 =>阵列(//指数的thread_id
        0 =>阵列(
            'ID'=> 85,
            THREAD_ID'=> 105775,
            '消息'= GT; 你好,
            'created_at'=> 5月20日,2015年
            '日期时间'=> 1432118191,
            SENDER_ID'=> 13,
        )
        1 =>阵列(
            'ID'=> 70,
            THREAD_ID'=> 105775,
            '消息'= GT; HII
            'created_at'=> 5月19日,2015年
            '日期时间'=> 1432021227,
            SENDER_ID'=> 13,
        )
    )
    224199 =>阵列(//指数的thread_id
        0 =>阵列(
            'ID'=> 88,
            THREAD_ID'=> 224199,
            '消息'= GT; yessss ...
            'created_at'=> 5月20日,2015年
            '日期时间'=> 1432306513,
            SENDER_ID'=> 14,
        )
        1 =>阵列(//指数的thread_id
            'ID'=> 75,
            THREAD_ID'=> 224199,
            '消息'= GT; hellowwww ...
            'created_at'=> 5月19日,2015年
            '日期时间'=> 1432021227,
            SENDER_ID'=> 14,
        )
    )
    107917 =>阵列(//指数的thread_id
        0 =>阵列(
            'ID'=> 56,
            THREAD_ID'=> 107917,
            '消息'= GT; 你好吗??,
            'created_at'=> 5月16日,2015年
            '日期时间'=> 1431792155,
            SENDER_ID'=> 14,
        )
        1 =>阵列(
            'ID'=> 30,
            THREAD_ID'=> 107917,
            '消息'= GT; 我很好。,
            'created_at'=> 2015年5月6日,
            '日期时间'=> 1430920006,
            SENDER_ID'=> 14,
        )
        2 =>阵列(
            'ID'=> 30,
            THREAD_ID'=> 107917,
            '消息'= GT; 你好么??,
            'created_at'=> 2015年5月6日,
            '日期时间'=> 1430920006,
            SENDER_ID'=> 14,
        )
    )
    378552 =>阵列(//指数的thread_id
        0 =>阵列(
            'ID'=> 108,
            THREAD_ID'=> 378552,
            '消息'= GT; 喜
            'created_at'=> 5月29日,2015年
            '日期时间'=> 1432906923,
            SENDER_ID'=> 14,
        )
        1 =>阵列(
            'ID'=> 107,
            THREAD_ID'=> 378552,
            '消息'= GT; 你好。
            'created_at'=> 5月29日,2015年
            '日期时间'=> 1432903194,
            SENDER_ID'=> 14,
        )
    )
);

所以我需要这样的输出:

  $ inboxMessages =阵列(
    378552 =>阵列(//指数的thread_id
        0 =>阵列(
            'ID'=> 108,
            THREAD_ID'=> 378552,
            '消息'= GT; 喜
            'created_at'=> 5月29日,2015年
            '日期时间'=> 1432906923,
            SENDER_ID'=> 14,
        )
        1 =>阵列(
            'ID'=> 107,
            THREAD_ID'=> 378552,
            '消息'= GT; 你好。
            'created_at'=> 5月29日,2015年
            '日期时间'=> 1432903194,
            SENDER_ID'=> 14,
        )
    )
    224199 =>阵列(//指数的thread_id
        0 =>阵列(
            'ID'=> 88,
            THREAD_ID'=> 224199,
            '消息'= GT; yessss ...
            'created_at'=> 5月20日,2015年
            '日期时间'=> 1432306513,
            SENDER_ID'=> 14,
        )
        1 =>阵列(//指数的thread_id
            'ID'=> 75,
            THREAD_ID'=> 224199,
            '消息'= GT; hellowwww ...
            'created_at'=> 5月19日,2015年
            '日期时间'=> 1432021227,
            SENDER_ID'=> 14,
        )
    )
    105775 =>阵列(//指数的thread_id
        0 =>阵列(
            'ID'=> 85,
            THREAD_ID'=> 105775,
            '消息'= GT; 你好,
            'created_at'=> 5月20日,2015年
            '日期时间'=> 1432118191,
            SENDER_ID'=> 13,
        )
        1 =>阵列(
            'ID'=> 70,
            THREAD_ID'=> 105775,
            '消息'= GT; HII
            'created_at'=> 5月19日,2015年
            '日期时间'=> 1432021227,
            SENDER_ID'=> 13,
        )
    )
    107917 =>阵列(//指数的thread_id
        0 =>阵列(
            'ID'=> 56,
            THREAD_ID'=> 107917,
            '消息'= GT; 你好吗??,
            'created_at'=> 5月16日,2015年
            '日期时间'=> 1431792155,
            SENDER_ID'=> 14,
        )
        1 =>阵列(
            'ID'=> 30,
            THREAD_ID'=> 107917,
            '消息'= GT; 我很好。,
            'created_at'=> 2015年5月6日,
            '日期时间'=> 1430920006,
            SENDER_ID'=> 14,
        )
        2 =>阵列(
            'ID'=> 30,
            THREAD_ID'=> 107917,
            '消息'= GT; 你好么??,
            'created_at'=> 2015年5月6日,
            '日期时间'=> 1430920006,
            SENDER_ID'=> 14,
        )
    ));

我要排序的日期时间子阵的thread_id 值主阵列。

的thread_id 已排序数组。

所以,我只想排序主阵列的thread_id 的子阵列。

我需要近期的日期时间是在阵列顶部

我试图让远不及它的作品对我来说:

 函数补偿($ A,$ B){
    如果($ A [0] - >的datetime == $ B [0] - > DATETIME){
        返回0;
    }
    返回$ a [0] - >&日期时间GT; $ B [0] - GT;日期时间? -1:1;
}uasort($ inboxMessages,补偿);

和还尝试用信息 ID

 函数补偿($ A,$ B){
    如果($一个[0] - 和SEQ ID == $ B [0] - 和SEQ ID){
        返回0;
    }
    返回$ a [0] - > ID> $ B [0] - 和SEQ ID? -1:1;
}uasort($ inboxMessages,补偿);


解决方案

您可以使用如下达到以下格式

 函数补偿($ A,$ B){
    如果($一个[0] [日期时间] == $ B [0] [日期时间']){
        返回0;
    }回美元[0] ['日期时间']> $ B [0] [日期时间] -1:1;
}
uasort($ inboxMessages,补偿);

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");
解决方案

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

这篇关于我要排序的PHP 3维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 09:29