合并来自单个数据集的关联数据

时间:2018-08-09 06:56:14

标签: php algorithm sorting multidimensional-array

我已经接管了一个已经包含大量数据的项目。 给定的格式不是最佳格式,仅包含单个数据集。它们大多数属于同一“事件”。我想合并这些数据集。

给定的数据集采用以下格式:

提供数据集:

[0] => Array
    (
        [id] => 4
        [title] => Party 1
        [start] => 2017-06-14
        [end] => 2017-06-14
        [className] => Array
            (
                [0] => yyy
                [1] => zzz
            )

        [color] => #26B99A
        [zeit] => xxx
    )

[1] => Array
    (
        [id] => 6
        [title] => Party 2
        [start] => 2017-04-27
        [end] => 2017-04-27
        [className] => Array
            (
                [0] => xxx
                [1] => yyy
            )

        [color] => #26B99A
        [zeit] => zzz
    )

[2] => Array
    (
        [id] => 7
        [title] => Party 2
        [start] => 2017-04-28
        [end] => 2017-04-28
        [className] => Array
            (
                [0] => xxx
                [1] => yyy
            )

        [color] => #26B99A
        [zeit] => zzz
    )

[3] => Array
    (
        [id] => 9
        [title] => Party 2
        [start] => 2017-04-29
        [end] => 2017-04-29
        [className] => Array
            (
                [0] => xxx
                [1] => yyy
            )

        [color] => #26B99A
        [zeit] => zzz
    )

[4] => Array
    (
        [id] => 11
        [title] => Party 3
        [start] => 2017-07-30
        [end] => 2017-07-30
        [className] => Array
            (
                [0] => xxx
                [1] => yyy
            )

        [color] => #26B99A
        [zeit] => zzz
    )

[5] => Array
    (
        [id] => 13
        [title] => Party 3
        [start] => 2017-07-31
        [end] => 2017-07-31
        [className] => Array
            (
                [0] => xxx
                [1] => yyy
            )

        [color] => #26B99A
        [zeit] => zzz
    )......

在这种情况下,将只有一个“第一方”,以及“第二方”和“第三方”的组合

因此,结果应如下所示:

预期结果

[0] => Array
(
    [id] => 4
    [title] => Party 1
    [start] => 2017-06-14
    [end] => 2017-06-14
    [className] => Array
        (
            [0] => yyy
            [1] => zzz
        )

    [color] => #26B99A
    [zeit] => xxx
)

[1] => Array
(
    [id] => 6
    [title] => Party 2
    [start] => 2017-04-27
    [end] => 2017-04-29    <---- end date edited
    [className] => Array
        (
            [0] => xxx
            [1] => yyy
        )

    [color] => #26B99A
    [zeit] => zzz
)


[2] => Array
(
    [id] => 11
    [title] => Party 3
    [start] => 2017-07-30
    [end] => 2017-07-31     <---- same
    [className] => Array
        (
            [0] => xxx
            [1] => yyy
        )

    [color] => #26B99A
    [zeit] => zzz
)

因此,我使用了一个PHP函数,该函数根据事件的标题对其进行格式化:

PHP:

function formateEvents($event_array){
$events = array();

foreach($event_array as $event)
{
    if(!isset($events[$event['title']]))
    {
        $events[$event['title']]['id']           = $event['id'];
        $events[$event['title']]['title']        = $event['title'];
        $events[$event['title']]['start']        = $event['start'];
        $events[$event['title']]['end']          = $event['end'];
        $events[$event['title']]['name']         = $event['title'];
        $events[$event['title']]['className']    = $event['className'];
        $events[$event['title']]['color']        = $event['color'];
        $events[$event['title']]['zeit']         = $event['zeit'];
    }else{


        if(strtotime($event['start']) < strtotime($events[$event['title']]['start']))
        {
            $events[$event['title']]['start']    = $event['start'];
        }

        if(strtotime($event['end']) > strtotime($events[$event['title']]['end']))
        {
            $events[$event['title']]['end']    = $event['end'];
        }
    }
}

return array_values($events);
}

这对于上面的数据集工作正常。 但是:问题是,我也有几年前的事件数据,它们通常具有相同的标题。在这种情况下,事件持续时间将超过几年。我要避免这种情况,只合并具有相同名称和相关日期的数据。

我想到的唯一方法是检查日子是否连续...像2018-01-01、2018-01-02、2018-01-03应该是同一事件...如果中间没有一天,则应视为另一事件

1 个答案:

答案 0 :(得分:1)

我的解决方案比我想要的复杂得多,但是可以正常工作。 所以,让我解释一下。

首先,我按事件的名称映射所有事件。

$eventsIndexedByTitleAndEndDate = array();
foreach ($event_array as $event) {
    $eventTitle = $event['title'];
    $eventEnd = $event['end'];

    $eventsIndexedByTitleAndEndDate[$eventTitle][$eventEnd] = $event;
}    

然后,我遍历此数据以查找要使用的日期,然后将此日期映射到另一个数组中,仍然使用事件的标题作为主要索引。

$eventDates = array();
foreach ($eventsIndexedByTitleAndEndDate as $eventTitle => $eventIndexedByTitle) {
    $datesFromIndexedTitle = (array_keys($eventIndexedByTitle));

    $start = $datesFromIndexedTitle[0];
    $end = $datesFromIndexedTitle[0];
    foreach ($datesFromIndexedTitle as $dateFromIndexedTitle) {
        $nextDayFromEnd = date('Y-m-d', strtotime("+1 day", strtotime($end)));
        if ($dateFromIndexedTitle == $nextDayFromEnd) {
            $end = $dateFromIndexedTitle;
        } elseif ($dateFromIndexedTitle > $nextDayFromEnd) {
            $eventDates[$eventTitle][] = [
                'start' => $start,
                'end' => $end,
            ];
            $start = $dateFromIndexedTitle;
            $end = $dateFromIndexedTitle;
        }
    }
    $eventDates[$eventTitle][] = [
        'start' => $start,
        'end' => $end,
    ];
}

之后,我已经映射了所有日期,我使用引用遍历了该数组,用Title索引的数组中的数据更新了键,并根据需要更新了开始/结束日期。< / p>

foreach ($eventDates as $title => &$eventDate) {
    foreach ($eventDate as &$eachEvent) {
        $start = $eachEvent['start'];
        $end = $eachEvent['end'];

        $eachEvent = $eventsIndexedByTitleAndEndDate[$title][$start];
        $eachEvent['start'] = $start;
        $eachEvent['end'] = $end;
    }
}

我相信可能有一种更简单的方法来执行此操作,但是我现在无法考虑一种解决方案,尽管这是一个很好的练习。 :)