3v4l.org

run code in 300+ PHP versions simultaneously
<?php $begin = 100; $end = 200; $array = [0 => 1, 10 => 2, 50 => 3, 70 => 2, 90 => 3, 110 => 4, 150 => 2, 170 => 3, 180 => 4, 190 => 2]; $array = array_reverse($array, true); $value = 2; $result = []; $prev_time = $end; foreach ($array as $time => $current_value) { if ($current_value == 2) { $result[] = [$time, $prev_time]; } $prev_time = $time; } var_dump($result);

preferences:
35.06 ms | 402 KiB | 5 Q