通过排序使用PHP日期第五维值的5维数组? [英] Sorting a 5 dimensional array by date in 5th dimension value using php?

查看:119
本文介绍了通过排序使用PHP日期第五维值的5维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用日期进行排序数组,但问题是,在键值的第一个数组存储 ADDED_DATE ,而第二个使用密钥 DATE_ADDED

我怎么可以按照日期进行排序(新元素第一)?

阵列的结构如下:

  [0] =>排列
    (
        [数据] =>排列
            (
                [0] =>排列
                    (
                        [媒体] => upcomingEvents_1214_1429325758.jpeg
                        [参考] =>即将举行的活动
                        [ADDED_DATE] => 2015年4月18日八时26分00秒
                        [类型] = GT;图像/ JPEG
                    )                [1] =>排列
                    (
                        [媒体] => upcomingEvents_1214_1429325809.jpeg
                        [参考] =>即将举行的活动
                        [ADDED_DATE] => 2015年4月18日8时26分51秒
                        [类型] = GT;图像/ JPEG
                    )                [2] =>排列
                    (
                        [媒体] => diary_1214_1434190391.jpeg
                        [参考] =>日记
                        [ADDED_DATE] => 2015年6月13日十五时43分11秒
                        [类型] = GT;图像/ JPEG
                    )            )        [名称] =>媒体
    )
 [1] =>排列
    (
        [数据] =>排列
            (
                [0] =>排列
                    (
                        [媒体] => image.jpg的
                        [media_thumb] =>
                        [couple_id] => 312
                        [DATE_ADDED] => 2015年6月22日8时38分09秒
                        [chat_type] =>图像/ JPEG
                    )                [1] =>排列
                    (
                        [媒体] => 1432787219556.jpg
                        [media_thumb] =>
                        [couple_id] => 312
                        [DATE_ADDED] => 2015年6月22日十七点45分45秒
                        [chat_type] =>图像/ JPEG
                    )                [2] =>排列
                    (
                        [媒体] => 1436160762565.jpg
                        [media_thumb] =>
                        [couple_id] => 312
                        [DATE_ADDED] => 2015-07-06 9时03分27秒
                        [chat_type] =>图像/ JPEG
                    )            )        [名称] =>聊
    )


解决方案

由于OP不说对输出数组structur我认为它应该是一样的输入结构什么。而且我认为,所有参赛作品必须由独立日期的类型(聊天或媒体)进行排序。

下面的算法需要三个步骤:


  • 减缩阵列成二维数组

  • 排序瘪阵列

  • 膨胀数组排序

这样做的advatage是,我们可以混合类型而不失结构进行排序。

code

  //数组放气
功能放气($ ARR)
{
    $瘪=阵列();
    的foreach($改编为$ dataGroup时){
        的foreach($ dataGroup时['数据']为$项目){
            $项目['标识'] = $ dataGroup时['标识'];
            $瘪[] = $项目;
        }
    }
    返回$瘪;
}//充气阵列
功能充气($ ARR)
{
    $充气=阵列();
    $ lastIdentifier = NULL;    的foreach($改编为$项目){
        如果($项目['标识']!= $ lastIdentifier){
            如果(使用isset($ dataGroup时)){
                $ dataGroup时['标识'] = $ lastIdentifier;
            }
            未设置($ dataGroup时);
            $ dataGroup时=阵列();
            $膨胀[] =&放大器; $ dataGroup时;
        }
        $ lastIdentifier = $项目['标识'];
        未设置($项目['标识']);
        $ dataGroup时['数据'] [] = $项目;
    }
    如果(使用isset($ dataGroup时)){
        $ dataGroup时['标识'] = $ lastIdentifier;
    }    返回$膨胀;
}//排序瘪阵列按日期
功能sortArray(安培; $ ARR)
{
    $回调函数=($ A,$ B)
    {
        如果(使用isset($ A ['ADDED_DATE'])){
            $ ADATE = $ A ['ADDED_DATE'];
        } ELSEIF(使用isset($ A ['DATE_ADDED'])){
            $ ADATE = $ A ['DATE_ADDED'];
        }其他{
            $ ADATE ='';
        }
        如果(使用isset($ B ['ADDED_DATE'])){
            $ bDate = $ B ['ADDED_DATE'];
        } ELSEIF(使用isset($ B ['DATE_ADDED'])){
            $ bDate = $ B ['DATE_ADDED'];
        }其他{
            $ bDate ='';
        }
        返回($ A日期< $ bDate)? 1:-1;
    };    usort($改编,$回调);
}
//测试输出的print_r($ ARR);// 1步:放气阵列
$ ARR =放气($ ARR);
//回声---瘪--- \\ n;
//的print_r($ ARR);// 2步:排序瘪阵列
sortArray($ ARR);
//回声---瘪和分类--- \\ n;
//的print_r($ ARR);// 3步:充气排序数组
$ ARR =膨胀($ ARR);
回声---排序,充气--- \\ n;
的print_r($ ARR);

您可以在打印行发表评论我要看到平板阵列之前和排序后。

测试输出

 阵列

    [0] =>排列
        (
            [数据] =>排列
                (
                    [0] =>排列
                        (
                            [媒体] => upcomingEvents_1214_1429325758.jpeg
                            [参考] =>即将举行的活动
                            [ADDED_DATE] => 2015年4月18日八时26分00秒
                            [类型] = GT;图像/ JPEG
                        )                    [1] =>排列
                        (
                            [媒体] => upcomingEvents_1214_1429325809.jpeg
                            [参考] =>即将举行的活动
                            [ADDED_DATE] => 2015年4月18日8时26分51秒
                            [类型] = GT;图像/ JPEG
                        )                    [2] =>排列
                        (
                            [媒体] => diary_1214_1434190391.jpeg
                            [参考] =>日记
                            [ADDED_DATE] => 2015年6月13日十五时43分11秒
                            [类型] = GT;图像/ JPEG
                        )                )            [名称] =>媒体
        )    [1] =>排列
        (
            [数据] =>排列
                (
                    [0] =>排列
                        (
                            [媒体] => image.jpg的
                            [media_thumb] =>
                            [couple_id] => 312
                            [DATE_ADDED] => 2015年6月22日8时38分09秒
                            [chat_type] =>图像/ JPEG
                        )                    [1] =>排列
                        (
                            [媒体] => 1432787219556.jpg
                            [media_thumb] =>
                            [couple_id] => 312
                            [DATE_ADDED] => 2015年6月22日十七点45分45秒
                            [chat_type] =>图像/ JPEG
                        )                    [2] =>排列
                        (
                            [媒体] => 1436160762565.jpg
                            [media_thumb] =>
                            [couple_id] => 312
                            [DATE_ADDED] => 2015-07-06 9时03分27秒
                            [chat_type] =>图像/ JPEG
                        )                )            [名称] =>聊
        ))
---分类和膨胀---
排列

    [0] =>排列
        (
            [数据] =>排列
                (
                    [0] =>排列
                        (
                            [媒体] => 1436160762565.jpg
                            [media_thumb] =>
                            [couple_id] => 312
                            [DATE_ADDED] => 2015-07-06 9时03分27秒
                            [chat_type] =>图像/ JPEG
                        )                    [1] =>排列
                        (
                            [媒体] => 1432787219556.jpg
                            [media_thumb] =>
                            [couple_id] => 312
                            [DATE_ADDED] => 2015年6月22日十七点45分45秒
                            [chat_type] =>图像/ JPEG
                        )                    [2] =>排列
                        (
                            [媒体] => image.jpg的
                            [media_thumb] =>
                            [couple_id] => 312
                            [DATE_ADDED] => 2015年6月22日8时38分09秒
                            [chat_type] =>图像/ JPEG
                        )                )            [名称] =>聊
        )    [1] =>排列
        (
            [数据] =>排列
                (
                    [0] =>排列
                        (
                            [媒体] => diary_1214_1434190391.jpeg
                            [参考] =>日记
                            [ADDED_DATE] => 2015年6月13日十五时43分11秒
                            [类型] = GT;图像/ JPEG
                        )                    [1] =>排列
                        (
                            [媒体] => upcomingEvents_1214_1429325809.jpeg
                            [参考] =>即将举行的活动
                            [ADDED_DATE] => 2015年4月18日8时26分51秒
                            [类型] = GT;图像/ JPEG
                        )                    [2] =>排列
                        (
                            [媒体] => upcomingEvents_1214_1429325758.jpeg
                            [参考] =>即将举行的活动
                            [ADDED_DATE] => 2015年4月18日八时26分00秒
                            [类型] = GT;图像/ JPEG
                        )                )            [名称] =>媒体
        ))

与所使用的阵列似乎没有什么大的区别,以解决只排序内部数组。但会有差异,如果你有哪些日期混合类型。例如媒体项比一个聊天项和另一媒体项是较旧的更新。在显示输出的所有媒体项目比聊天项以上。


由于AVGP和spendious。他们启发了我我的解决方案与他们的答案: AVGP的平板解决方案并的内阵列的spendious的排序。

I need to sort an array using dates, but the problem is that the first array stores that value in the key added_date while the second one uses the key date_added.

How can I sort them according to date (newer elements first)?

The array has the following structure:

[0] => Array
    (
        [data] => Array
            (
                [0] => Array
                    (
                        [media] => upcomingEvents_1214_1429325758.jpeg
                        [reference] => upcomingEvents
                        [added_date] => 2015-04-18 08:26:00
                        [type] => image/jpeg
                    )

                [1] => Array
                    (
                        [media] => upcomingEvents_1214_1429325809.jpeg
                        [reference] => upcomingEvents
                        [added_date] => 2015-04-18 08:26:51
                        [type] => image/jpeg
                    )

                [2] => Array
                    (
                        [media] => diary_1214_1434190391.jpeg
                        [reference] => diary
                        [added_date] => 2015-06-13 15:43:11
                        [type] => image/jpeg
                    )

            )

        [identifier] => media
    )
 [1] => Array
    (
        [data] => Array
            (
                [0] => Array
                    (
                        [media] => image.jpg
                        [media_thumb] => 
                        [couple_id] => 312
                        [date_added] => 2015-06-22 08:38:09
                        [chat_type] => image/jpeg
                    )

                [1] => Array
                    (
                        [media] => 1432787219556.jpg
                        [media_thumb] => 
                        [couple_id] => 312
                        [date_added] => 2015-06-22 17:45:45
                        [chat_type] => image/jpeg
                    )

                [2] => Array
                    (
                        [media] => 1436160762565.jpg
                        [media_thumb] => 
                        [couple_id] => 312
                        [date_added] => 2015-07-06 09:03:27
                        [chat_type] => image/jpeg
                    )

            )

        [identifier] => chat
    )

解决方案

As the OP does not say anything about the array structur of the output I assume that it should be the same as the input structure. Further I think all entries should be sorted by date independently of their type (chat or media).

The following algorithm takes three steps:

  • Deflate the array into a two dimensional array
  • Sort the deflated array
  • Inflate the sorted array

The advatage of doing so is that we can mix the types and sort them without losing the structure.

Code

// Deflate array
function deflate($arr)
{
    $deflated = array();
    foreach($arr as $dataGroup) {
        foreach ($dataGroup['data'] as $item) {
            $item['identifier'] = $dataGroup['identifier'];
            $deflated[] = $item;
        }
    }
    return $deflated;
}

// Inflate array
function inflate($arr)
{
    $inflated = array();
    $lastIdentifier = NULL;

    foreach ($arr as $item) {
        if ($item['identifier'] != $lastIdentifier) {
            if (isset($dataGroup)) {
                $dataGroup['identifier'] = $lastIdentifier;
            }
            unset($dataGroup);
            $dataGroup = array();
            $inflated[] = &$dataGroup;
        }
        $lastIdentifier = $item['identifier'];
        unset($item['identifier']);
        $dataGroup['data'][] = $item;
    }
    if (isset($dataGroup)) {
        $dataGroup['identifier'] = $lastIdentifier;
    }

    return $inflated;
}

// Sort deflated array by date
function sortArray(&$arr)
{
    $callback = function($a, $b)
    {
        if(isset($a['added_date'])) {
            $aDate = $a['added_date'];
        } elseif(isset($a['date_added'])) {
            $aDate = $a['date_added'];
        } else {
            $aDate = '';
        }
        if(isset($b['added_date'])) {
            $bDate = $b['added_date'];
        } elseif(isset($b['date_added'])) {
            $bDate = $b['date_added'];
        } else {
            $bDate = '';
        }
        return ($aDate < $bDate) ? 1 : -1;
    };

    usort($arr, $callback);
}


// Test output

print_r($arr);

// 1. step: deflate array
$arr = deflate($arr);
// echo "--- deflated ---\n";
// print_r($arr);

// 2. step: sort deflated array
sortArray($arr);
// echo "--- deflated and sorted ---\n";
// print_r($arr);

// 3. step: inflate sorted array
$arr = inflate($arr);
echo "--- sorted and inflated ---\n";
print_r($arr);

You can comment in the print lines to see the flat array before and after sorting.

Output of the test

Array
(
    [0] => Array
        (
            [data] => Array
                (
                    [0] => Array
                        (
                            [media] => upcomingEvents_1214_1429325758.jpeg
                            [reference] => upcomingEvents
                            [added_date] => 2015-04-18 08:26:00
                            [type] => image/jpeg
                        )

                    [1] => Array
                        (
                            [media] => upcomingEvents_1214_1429325809.jpeg
                            [reference] => upcomingEvents
                            [added_date] => 2015-04-18 08:26:51
                            [type] => image/jpeg
                        )

                    [2] => Array
                        (
                            [media] => diary_1214_1434190391.jpeg
                            [reference] => diary
                            [added_date] => 2015-06-13 15:43:11
                            [type] => image/jpeg
                        )

                )

            [identifier] => media
        )

    [1] => Array
        (
            [data] => Array
                (
                    [0] => Array
                        (
                            [media] => image.jpg
                            [media_thumb] => 
                            [couple_id] => 312
                            [date_added] => 2015-06-22 08:38:09
                            [chat_type] => image/jpeg
                        )

                    [1] => Array
                        (
                            [media] => 1432787219556.jpg
                            [media_thumb] => 
                            [couple_id] => 312
                            [date_added] => 2015-06-22 17:45:45
                            [chat_type] => image/jpeg
                        )

                    [2] => Array
                        (
                            [media] => 1436160762565.jpg
                            [media_thumb] => 
                            [couple_id] => 312
                            [date_added] => 2015-07-06 09:03:27
                            [chat_type] => image/jpeg
                        )

                )

            [identifier] => chat
        )

)
--- sorted and inflated ---
Array
(
    [0] => Array
        (
            [data] => Array
                (
                    [0] => Array
                        (
                            [media] => 1436160762565.jpg
                            [media_thumb] => 
                            [couple_id] => 312
                            [date_added] => 2015-07-06 09:03:27
                            [chat_type] => image/jpeg
                        )

                    [1] => Array
                        (
                            [media] => 1432787219556.jpg
                            [media_thumb] => 
                            [couple_id] => 312
                            [date_added] => 2015-06-22 17:45:45
                            [chat_type] => image/jpeg
                        )

                    [2] => Array
                        (
                            [media] => image.jpg
                            [media_thumb] => 
                            [couple_id] => 312
                            [date_added] => 2015-06-22 08:38:09
                            [chat_type] => image/jpeg
                        )

                )

            [identifier] => chat
        )

    [1] => Array
        (
            [data] => Array
                (
                    [0] => Array
                        (
                            [media] => diary_1214_1434190391.jpeg
                            [reference] => diary
                            [added_date] => 2015-06-13 15:43:11
                            [type] => image/jpeg
                        )

                    [1] => Array
                        (
                            [media] => upcomingEvents_1214_1429325809.jpeg
                            [reference] => upcomingEvents
                            [added_date] => 2015-04-18 08:26:51
                            [type] => image/jpeg
                        )

                    [2] => Array
                        (
                            [media] => upcomingEvents_1214_1429325758.jpeg
                            [reference] => upcomingEvents
                            [added_date] => 2015-04-18 08:26:00
                            [type] => image/jpeg
                        )

                )

            [identifier] => media
        )

)

With the used array it seems that there is no big difference to a solution which only sorts the inner array. But there will be a difference, if you have dates which mix the types. For example a media item which is newer than a chat item and another media item which is older. In the shown output all media items are older than the chat items.


Thanks to AVGP and spendious. They inspired me to my solution with their answers: AVGP's flat solution and spendious's sorting of the inner arrays.

这篇关于通过排序使用PHP日期第五维值的5维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆