How can I effectively determine whether there are intersection data ranges?

Source: Internet
Author: User
How can I effectively determine whether there are intersection of several sets of data ranges? php

5000 ~ 7000
2000 ~ 6000
8000 ~ 9000
4000 ~ 8500
5000 ~ 7000
In php, how can we more effectively judge whether there are duplicates of these groups of random data?

Expected value:
2000 ~ 4000
4000 ~ 5000
5000 ~ 6000
6000 ~ 7000
7000 ~ 8500
8500 ~ 9000


Reply to discussion (solution)

This post was last edited by xuzuning at 09:00:13, January 9 ,.

$ A = array (5000,700 0), array (2000,600 0), array (8000,900 0), array (4000,850 0), array (5000,700 0 ),); $ r = array (); foreach ($ a as $ v) {$ r [] = $ v [0]; $ r [] = $ v [1];} $ r = array_unique ($ r); sort ($ r); $ a = array (); for ($ I = 0; $ I
 
  
Array
  
(
[0] => Array
(
[0] = & gt; 2000
[1] => 4000.
)

[1] => Array
(
[0] = & gt; 4000
[1] => 5000.
)

[2] => Array
(
[0] = & gt; 5000
[1] => 6000.
)

[3] => Array
(
[0] = & gt; 6000
[1] => 7000.
)

[4] => Array
(
[0] = & gt; 7000
[1] => 8000.
)

[5] => Array
(
[0] = & gt; 8000
[1] => 8500.
)

[6] => Array
(
[0] = & gt; 8500
[1] => 9000.
)

)

It's really fast. thank you, moderator.

I want to see if there are repeated flags for these data sets,
If there are repeated values, false is returned,
Returns true if no duplicates exist,
No need to generate a new non-repeated array.
Array (
Array (5000,700 0 ),
Array (2000,600 0 ),
Array (8000,900 0 ),
Array (4000,850 0 ),
Array (5000,700 0 ),
);

In this case, no duplicates are returned and true is expected to be returned.
Array (
Array (2000,300 0 ),
Array (3000,400 0 ),
Array (4000,500 0)
);

    "green", "b" => "brown", "c" => "blue", "red");$array2 = array("a" => "green", "b" => "yellow", "blue", "red");$result_array = array_intersect_assoc($array1, $array2);print_r($result_array);?>


Judge whether $ result_array is null

    "green", "b" => "brown", "c" => "blue", "red");$array2 = array("a" => "green", "b" => "yellow", "blue", "red");$result_array = array_intersect_assoc($array1, $array2);print_r($result_array);?>


Judge whether $ result_array is null



Array_intersect_assoc
This two-dimensional array cannot be determined,
In addition, this case is not within the intersection range.
Array (
Array (2000,300 0 ),
Array (3000,400 0 ),
Array (4000,500 0)
);

Won't you change it?
If the original array already exists, the generated non-repeating array must be larger than the original array.

Find the specific scope of the program by moderator
Alas, simply seeking true or false is easier:
Compare the minimum values of any two items with the maximum values of the other item. once there is an intersection-> break loop, if the loop ends, there is no intersection.

Won't you change it?
If the original array already exists, the generated non-repeating array must be larger than the original array.

Thank you, moderator.
By re-sorting, a new non-repeating array is re-generated,
If the number of elements in the non-repeated array changes, the original array has an intersection, right?

The flag does not seem to be correctly judged in the following two cases:
$ A = array (
Array (5000,700 0 ),
Array (6000,820 0 ),
Array (8000,900 0)
);

$ A = array (
Array (5000,550 0 ),
Array (6000,650 0 ),
Array (8000,850 0)
);

There are no more effective methods.


You only need to determine whether the content is included. you can write it as a function.
Continue with 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;}

You only need to determine whether the content is included. you can write it as a function.
Continue with 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;}



This sentence is wonderful. Thank you, moderator.
Array_search ($ v [0], $ r )! = Array_search ($ v [1], $ r)-1

The above code
Array (
Array (2000,300 0 ),
Array (3000,400 0 ),
Array (4000,500 0)
);
You can simply consider the situation.
Thank you very much. M (_) m

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.