There are 2 arrays: $times and $zhurnal

The task: to remove from the $times array all elements that are in the $zhurnal range of elements, that is, the keys of the arrays are the beginning of the range, and the values ​​are the end of the range.

Example of $times array:

 $times = [ [600 => 660], [660 => 720], [720 => 780], [780 => 840], [840 => 900], [900 => 960], [960 => 1020], [1020 => 1080], [1080 => 1140], [1140 => 1200], [1200 => 1260] ]; 

An example of the $zhurnal (the numbers can be in any order):

 $zhurnal = [ [660 => 720], [600 => 660] ]; 

Thank you in advance!

    1 answer 1

    You need to remove from the $times array the elements that are in the $zhurnal range of elements, if I understand you correctly, then try this option:

     function array_diff_gaps($array1, $array2) { $prevnext = function($a) { return array_map(function($j) { return array_merge(array_keys($j), array_values($j)); }, $a); }; $array2 = array_reduce($prevnext($array2), 'array_merge', []); foreach($prevnext($array1) as $key => $value) { foreach ($array2 as $k => $v) { if (in_array($v, range($value[0], $value[1]))) unset($array1[$key]); } } return $array1; } 

    An example of using the function array_diff_gaps :

     $times = [ [600 => 660], [660 => 720], [720 => 780], [780 => 840], [840 => 900], [900 => 960], [960 => 1020], [1020 => 1080], [1080 => 1140], [1140 => 1200], [1200 => 1260] ]; $zhurnal = [ [660 => 720], [600 => 660] ]; print_r(array_diff_gaps($times, $zhurnal)); 

    We get the following result ( see demo )

    • And if the values ​​are [[640 => 650], [680 => 800]], then it no longer works - vadikbrooks
    • @ webdelo24, give an example of two arrays, and the expected result - Let's say Pie
    • @ lets-say-pie I wrote the example in question, just the values ​​are constantly changing in keys and values. The point is that there are ranges: the key is the beginning of the range, the value is the end. For example, the array $ zhurnal [660 => 720], the array $ times [[640 => 650], [680 => 800]], you should do this: if any element of the array $ times is in the range 660 => 720 (maybe do not completely enter, but only touch), then this element needs to be removed - vadikbrooks
    • @ webdelo24, look, updated answer. - Let's say Pie
    • @ webdelo24, if something is wrong, or have any questions, write. - Let's say Pie