如何在php中检查数组中的重复值

时间:2015-05-07 09:18:55

标签: php arrays codeigniter datetime

我有这样的数组:

     Array(
        [0] => Array
            (
                [timesheet_id] => 27
                [fullname] => Technician Nguyen
                [division_id] => 1
                [date_ts] => Thursday
                [user_id] => 3
                [from_time] => 2
                [to_time] => 11.3
                [from_time_ot] => 0
                [to_time_ot] => 6
                [from_time_wt] => 8.3
                [to_time_wt] => 17.3
                [time] => 9.3
                [OT_table] => 2
            )

        [1] => Array
            (
                [timesheet_id] => 27
                [fullname] => Technician Nguyen
                [division_id] => 1
                [date_ts] => Thursday
                [user_id] => 3
                [from_time] => 2
                [to_time] => 11.3
                [from_time_ot] => 6
                [to_time_ot] => 8.3
                [from_time_wt] => 8.3
                [to_time_wt] => 17.3
                [time] => 9.3
                [OT_table] => 1.5
            )

        [2] => Array
            (
                [timesheet_id] => 32
                [fullname] => Technician Nguyen
                [division_id] => 1
                [date_ts] => Friday
                [user_id] => 3
                [from_time] => 17.3
                [to_time] => 22.3
                [from_time_ot] => 17.3
                [to_time_ot] => 23.5
                [from_time_wt] => 8.3
                [to_time_wt] => 17.3
                [time] => 5
                [OT_table] => 1.5
            ))

我希望以这种方式使用相同的数组,这意味着它将检查是否在同一天然后检查一段时间(本案例重点关注" from_time_ot和to_time_ot")并输出时间范围"时间和结束时间#34;比如下面的数组。

  Array(
        [0] => Array
            (
                [timesheet_id] => 27
                [fullname] => Technician Nguyen
                [division_id] => 1
                [date_ts] => Thursday
                [user_id] => 3
                [from_time] => 2
                [to_time] => 11.3
                [from_time_ot] => 0
                [to_time_ot] => 8.3
                [from_time_wt] => 8.3
                [to_time_wt] => 17.3
                [time] => 9.3
                [OT_table] => 2
            )

        [1] => Array
            (
                [timesheet_id] => 32
                [fullname] => Technician Nguyen
                [division_id] => 1
                [date_ts] => Friday
                [user_id] => 3
                [from_time] => 17.3
                [to_time] => 22.3
                [from_time_ot] => 17.3
                [to_time_ot] => 23.5
                [from_time_wt] => 8.3
                [to_time_wt] => 17.3
                [time] => 5
                [OT_table] => 1.5
            ))

任何人都可以帮我这个吗?

2 个答案:

答案 0 :(得分:0)

这只是一个解决方法,您可以根据您的规范进行修改

这只是一个简单的

$data = array(
         array(
                'timesheet_id' => 27,
                'fullname' => 'Technician Nguyen',
                'division_id' => 1,
                'date_ts' => 'Thursday',
                'user_id' => 3,
                'from_time' => 2,
                'to_time' => 11.3,
                'from_time_ot' => 0,
                'to_time_ot' => 6,
                'from_time_wt' => 8.3,
                'to_time_wt' => 17.3,
                'time' => 9.3,
                'OT_table' => 2
            ),
        array(
                'timesheet_id' => 27,
                'fullname' => 'Technician Nguyen',
                'division_id' => 1,
                'date_ts' => 'Thursday',
                'user_id' => 3,
                'from_time' => 2,
                'to_time' => 11.3,
                'from_time_ot' => 6,
                'to_time_ot' => 8.3,
                'from_time_wt' => 8.3,
                'to_time_wt' => 17.3,
                'time' => 9.3,
                'OT_table' => 1.5
            ),
        array(
                'timesheet_id' => 32,
                'fullname' => 'Technician Nguyen',
                'division_id' => 1,
                'date_ts' => 'Friday',
                'user_id' => 3,
                'from_time' => 17.3,
                'to_time' => 22.3,
                'from_time_ot' => 17.3,
                'to_time_ot' => 23.5,
                'from_time_wt' => 8.3,
                'to_time_wt' => 17.3,
                'time' => 5,
                'OT_table' => 1.5
            )
);

function arrayUnique($array)  
{  

    // Unique Array for return  
    $arrayRewrite = array();  
    // Array with the md5 hashes  
    $arrayHashes = array();  

    $check = array();

    foreach($array as $key => $item) {  
        // checker
        $timesheet_id = $item['timesheet_id'];
        // check if already existing in the record
        if (! in_array($timesheet_id, $check)) {
            array_push($check, $timesheet_id);
            array_push($arrayRewrite, $item);
        }

    }  
    return $arrayRewrite;  
}  


$new_data = arrayUnique($data);


print_r($new_data);

输出:

Array
(
    [0] => Array
        (
            [timesheet_id] => 27
            [fullname] => Technician Nguyen
            [division_id] => 1
            [date_ts] => Thursday
            [user_id] => 3
            [from_time] => 2
            [to_time] => 11.3
            [from_time_ot] => 0
            [to_time_ot] => 6
            [from_time_wt] => 8.3
            [to_time_wt] => 17.3
            [time] => 9.3
            [OT_table] => 2
        )

    [1] => Array
        (
            [timesheet_id] => 32
            [fullname] => Technician Nguyen
            [division_id] => 1
            [date_ts] => Friday
            [user_id] => 3
            [from_time] => 17.3
            [to_time] => 22.3
            [from_time_ot] => 17.3
            [to_time_ot] => 23.5
            [from_time_wt] => 8.3
            [to_time_wt] => 17.3
            [time] => 5
            [OT_table] => 1.5
        )

)

答案 1 :(得分:0)

以下代码将遍历您的数组并构建一个不包含重复元素的新数组。

<?php

$array = [['a', 'b'], ['a', 'b'], ['q', 'a']];

$results = [];
for ($i = 0; $i < count($array); $i++) {
    for ($j = 0; $j < count($array); $j++) {
        if ($i === $j) {
            continue;
        }

        if ($array[$i] !== $array[$j] && !in_array($array[$i], $results)) {
            $results[] = $array[$i];
        }
    }
}

var_dump($results); //output [['a', 'b'], ['q', 'a']]

希望这会有所帮助:)