Ask how to effectively determine whether several sets of data range has the intersection

Source: Internet
Author: User
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;
}
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.