How to effectively determine whether several sets of data range has the intersection
5000~7000
2000~6000
8000~9000
4000~8500
5000~7000
How are these random sets of data in PHP more effective in judging if there are duplicates?
Expectations are
2000~4000
4000~5000
5000~6000
6000~7000
7000~8500
8500~9000
PHP
Share to:
------Solution--------------------
Only need to distinguish whether it is included, can be written as function
or follow the beginning of the idea
$a = Array (
Array (5000, 7000),
Array (2000, 6000),
Array (8000, 9000),
Array (4000, 8500),
Array (5000, 7000),
);
Var_dump (foo ($a)); BOOL (FALSE)
$a = Array (
Array (5000, 7000),
Array (6000, 8200),
Array (8000, 9000)
);
Var_dump (foo ($a)); BOOL (FALSE)
$a = Array (
Array (5000, 5500),
Array (6000, 6500),
Array (8000, 8500)
);
Var_dump (foo ($a)); BOOL (TRUE)
function foo ($a) {
$r = Array_unique (Call_user_func_array (' Array_merge ', $a));
Sort ($r);
foreach ($a as $v) {
if (Array_search ($v [0], $r)! = Array_search ($v [1], $r)-1) return false;
}
return true;
}