如何在php中检查数组中的重复值
我有这样一个数组:如何在php中检查数组中的重复值,php,arrays,codeigniter,datetime,Php,Arrays,Codeigniter,Datetime,我有这样一个数组: Array( [0] => Array ( [timesheet_id] => 27 [fullname] => Technician Nguyen [division_id] => 1 [date_ts] => Thursday [use
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
))
我希望以这种方式使用相同的数组,这意味着它将检查是否在同一天,然后检查时间段(本例的重点是“从时间到时间”),并输出“从时间到结束时间”的时间范围,如下面的数组所示
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
))
有人可以帮我吗?这只是一个解决方案,您可以根据您的规范进行修改 这只是一个简单的问题 输出:
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
)
)
下面的代码将在数组上循环并构建一个不包含重复元素的新数组
<?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']]
以及您尝试过的内容。如果在第1个from=0到=6,第2个from=10到12,该怎么办?或者在第一个from=0到=14之间,在第二个from=10到12之间?那么其他元素怎么办呢?foreach
和if
将帮助你。准确地检查这篇文章,我想计算总的正常工作时间:-如果在第一个从=0到=6,在第二个从=10到12:也应该检查工作时间,如果工作时间从8h30到17h30,输出为:实际时间也取决于8h30-17h30的工作时间,过滤时间为:10-12,总小时数为:2小时。
<?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']]