【问题标题】:Checking Duplicate values and merge them php mysql检查重复值并将它们合并php mysql
【发布时间】:2021-01-10 15:31:39
【问题描述】:

我如何知道重复值,并删除它,

这是我的代码...

function _get($limit=NULL)
    {
        $results = array();
        $single = array();
        $this->db->select("MAX(schedule.start_at) as max_start, MAX(schedule.end_at) as max_end");
        $this->db->group_by(array("schedule.start_at", "schedule.end_at"));
        $this->db->from("schedule2_tbl as schedule");
        $q = $this->db->get();

        if( $q->num_rows() ) 
        {
            $data = $q->result_array();
            // return $data;

            foreach($data as $key => $row){
                $single[] = $row;
            }
            $results = $single;
            return $results;

        }
        
    }

这是我的结果,

{
"max_start": "2020-07-02 05:30:00",
"max_end": "2020-07-02 06:30:00"
},
{
"max_start": "2020-07-02 06:30:00",
"max_end": "2020-07-02 07:00:00"
},
{
"max_start": "2020-07-02 06:30:00",
"max_end": "2020-07-02 07:30:00"
},
{
"max_start": "2020-07-02 07:00:00",
"max_end": "2020-07-02 07:30:00"
}

如果max_start、max_end已经存在,则取最大值。 例如,在 max_start 06:30:00 和 max_end 07:30:00 的情况下, 一共有三个值,

{
"max_start": "2020-07-02 06:30:00",
"max_end": "2020-07-02 07:00:00"
},
{
"max_start": "2020-07-02 06:30:00",
"max_end": "2020-07-02 07:30:00"
},
{
"max_start": "2020-07-02 07:00:00",
"max_end": "2020-07-02 07:30:00"
}

在这种情况下,我只需要存储 max_start 的最小值和 max_end 的最大值, 那是 06:30:00 到 07:30:00。

请帮忙。

更新

当我向数组添加更多值时,第一个数组显示错误。 显示出来了,

2020-07-02 05:30:00 - 2020-07-02 07:30:00 
2020-07-02 07:30:00 - 2020-07-02 08:30:00..... 

但是,它应该显示,

2020-07-02 05:30:00 - 2020-07-02 06:30:00 
2020-07-02 06:30:00 - 2020-07-02 07:30:00.....

这是我的代码

$initialData = $data = [
                                        [
                                        "max_start": "2020-07-02 05:30:00",
                                        "max_end": "2020-07-02 06:30:00"
                                        ],
                                        [
                                        "max_start": "2020-07-02 06:00:00",
                                        "max_end": "2020-07-02 07:30:00"
                                        ],
                                        [
                                        "max_start": "2020-07-02 06:30:00",
                                        "max_end": "2020-07-02 07:00:00"
                                        ],
                                        [
                                        "max_start": "2020-07-02 06:30:00",
                                        "max_end": "2020-07-02 07:30:00"
                                        ],
                                        [
                                        "max_start": "2020-07-02 07:00:00",
                                        "max_end": "2020-07-02 07:30:00"
                                        ],
                                        [
                                        "max_start": "2020-07-02 07:30:00",
                                        "max_end": "2020-07-02 08:30:00"
                                        ],
                                        [
                                        "max_start": "2020-07-02 08:30:00",
                                        "max_end": "2020-07-02 09:30:00"
                                        ],
                                        [
                                        "max_start": "2020-07-02 09:30:00",
                                        "max_end": "2020-07-02 11:15:00"
                                        ],
                                        [
                                        "max_start": "2020-07-02 11:15:00",
                                        "max_end": "2020-07-02 11:30:00"
                                        ],
                                        [
                                        "max_start": "2020-07-02 11:30:00",
                                        "max_end": "2020-07-02 12:00:00"
                                        ],
                                        [
                                        "max_start": "2020-07-02 11:30:00",
                                        "max_end": "2020-07-02 12:30:00"
                                        ],
                                        [
                                        "max_start": "2020-07-02 12:00:00",
                                        "max_end": "2020-07-02 12:30:00"
                                        ],
                                        [
                                        "max_start": "2020-07-02 12:30:00",
                                        "max_end": "2020-07-02 13:00:00"
                                        ],
                                        [
                                        "max_start": "2020-07-02 12:30:00",
                                        "max_end": "2020-07-02 13:30:00"
                                        ],
                                        [
                                        "max_start": "2020-07-02 13:00:00",
                                        "max_end": "2020-07-02 13:30:00"
                                        ]
                                    ];

            // Order the list chronologically by the "max_start" value, to make comparison easier later
            usort($data, function($a, $b){
                return $a['max_start'] <=> $b['max_start'];
            });


            // Final result will be collected here
            $result = [];

            // Work with the first list value as long there is one
            while ($currentInterval = array_shift($data)) {
                
                // Compare with each other value in the list
                foreach ($data as $index => $interval) {
                    
                    // Check if intervals overlap
                    // Replace "<" with a "<=" if you want to merge intervals that "touch": one interval ends at the same time another one begins
                    if ($interval['max_start'] < $currentInterval['max_end']) {

                        // Merge when needed
                        $currentInterval['max_end'] = max ($currentInterval['max_end'], $interval['max_end']);
                        
                        // Remove the merged interval
                        unset($data[$index]);
                        
                    }
                }
                
                // Add to result
                $result[] = $currentInterval;
            }
            return $result; 

这是我的结果:

[
[
"max_start": "2020-07-02 05:30:00",
"max_end": "2020-07-02 07:30:00"
],
[
"max_start": "2020-07-02 07:30:00",
"max_end": "2020-07-02 08:30:00"
],
[
"max_start": "2020-07-02 08:30:00",
"max_end": "2020-07-02 09:30:00"
],
[
"max_start": "2020-07-02 09:30:00",
"max_end": "2020-07-02 11:15:00"
],
[
"max_start": "2020-07-02 11:15:00",
"max_end": "2020-07-02 11:30:00"
],
[
"max_start": "2020-07-02 11:30:00",
"max_end": "2020-07-02 12:30:00"
],
[
"max_start": "2020-07-02 12:30:00",
"max_end": "2020-07-02 13:30:00"
]
]

之前的代码:

$initialData = $data = $q->result_array();

            // Order the list chronologically by the "max_start" value, to make comparison easier later
            usort($data, function($a, $b){
                return $a['max_start'] <=> $b['max_start'];
            });


            // Final result will be collected here
            $result = [];

            // Work with the first list value as long there is one
            while ($currentInterval = array_shift($data)) {
                
                // Compare with each other value in the list
                foreach ($data as $index => $interval) {
                    
                    // Check if intervals overlap
                    // Replace "<" with a "<=" if you want to merge intervals that "touch": one interval ends at the same time another one begins
                    if ($interval['max_start'] < $currentInterval['max_end']) {

                        // Merge when needed
                        $currentInterval['max_end'] = max ($currentInterval['max_end'], $interval['max_end']);
                        
                        // Remove the merged interval
                        unset($data[$index]);
                        
                    }
                }
                
                // Add to result
                $result[] = $currentInterval;
}
return $result;

【问题讨论】:

    标签: php mysql arrays codeigniter


    【解决方案1】:

    这是一个适用于您发布的数据格式的 sn-p。

    $initialData = $data = [
      [
        'max_start' => '2020-07-02 05:30:00',
        'max_end' => '2020-07-02 06:30:00',
      ],
      [
        'max_start' => '2020-07-02 07:00:00',
        'max_end' => '2020-07-02 07:30:00',
      ],
      [
        'max_start' => '2020-07-02 06:30:00',
        'max_end' => '2020-07-02 07:00:00',
      ],
      [
        'max_start' => '2020-07-02 06:30:00',
        'max_end' => '2020-07-02 07:30:00',
      ]
    ];
    
    // Order the list chronologically by the "max_start" value, to make comparison easier later
    usort($data, function($a, $b){
        return $a['max_start'] <=> $b['max_start'];
    });
    
    
    // Final result will be collected here
    $result = [];
    
    // Work with the first list value as long there is one
    while ($currentInterval = array_shift($data)) {
        
        // Compare with each other value in the list
        foreach ($data as $index => $interval) {
            
            // Check if intervals start at the same time
            if ($interval['max_start'] == $currentInterval['max_start']) {
    
                // Merge when needed
                $currentInterval['max_end'] = max ($currentInterval['max_end'], $interval['max_end']);
                
                // Remove the merged interval
                unset($data[$index]);
                
            }
        }
        
        // Add to result
        $result[] = $currentInterval;
    }
    
    echo 'Initial list: ', PHP_EOL, print_r($initialData, true);
    echo 'Merged list: ', PHP_EOL, print_r($result, true);
    

    这个 sn-p 有以下输出:

    Initial list: 
    Array
    (
        [0] => Array
            (
                [max_start] => 2020-07-02 05:30:00
                [max_end] => 2020-07-02 06:30:00
            )
    
        [1] => Array
            (
                [max_start] => 2020-07-02 07:00:00
                [max_end] => 2020-07-02 07:30:00
            )
    
        [2] => Array
            (
                [max_start] => 2020-07-02 06:30:00
                [max_end] => 2020-07-02 07:00:00
            )
    
        [3] => Array
            (
                [max_start] => 2020-07-02 06:30:00
                [max_end] => 2020-07-02 07:30:00
            )
    
    )
    Merged list: 
    Array
    (
        [0] => Array
            (
                [max_start] => 2020-07-02 05:30:00
                [max_end] => 2020-07-02 06:30:00
            )
    
        [1] => Array
            (
                [max_start] => 2020-07-02 06:30:00
                [max_end] => 2020-07-02 07:30:00
            )
    
        [2] => Array
            (
                [max_start] => 2020-07-02 07:00:00
                [max_end] => 2020-07-02 07:30:00
            )
    
    )
    

    如果适合您的需要或需要进一步调整,请告诉我。


    对于7.0 之前的PHP 版本,将usort 代码替换为以下代码:

    usort($data, function($a, $b){
    
        if ($a['max_start'] == $b['max_start']) {
            return 0;
        }
    
        return $a['max_start'] > $b['max_start'] ? -1 : 1;
    });
    

    请注意,PHP 5.6 已于 2018 年 12 月 31 日达到其生命周期结束状态,不建议再使用它。

    【讨论】:

    • 您好,首先非常感谢您的回答。我已经更新了我的问题...当我更新数组第一个值出错时,显示 2020-07-02 05:30:00 - 2020-07-02 07:30:00 然后 2020-07-02 07:30: 00 到 2020-07-02 08:30:00 ..... 它应该显示 2020-07-02 05:30:00 - 2020-07-02 06:30:00 然后 2020-07-02 06:30 :00 to 2020-07-02 07:30:00..... 请看一下。
    • 问题出在这个数组[ 'max_start' =&gt; '2020-07-02 06:00:00', 'max_end' =&gt; '2020-07-02 07:30:00' ],
    • 我想我误解了你的问题。我以为您想合并 any and all 重叠间隔,但现在我看到您只想合并具有相同起点的间隔。请让我知道这个解释是否正确,我会更新我的代码。
    • 如果对您有帮助或需要进一步调整,请告诉我。
    • 我已将我的 php 版本更新到 7.0 并修复了问题....
    猜你喜欢
    • 2019-03-17
    • 2018-03-27
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    • 2023-02-23
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多