$arr = [
//...
11 => true,
12 => true,
15 => false,
16 => true,
17 => false
18 => false
//...
];
$response = [
0 => "11 - 12 true",
1 => "15 - 15 false",
3 => "16 - 16 true",
4 => "17 - 18 false"
];
$arr = [
11 => true,
12 => true,
15 => false,
16 => true,
17 => false,
18 => false
];
reset($arr);
$key_from = key($arr);
$key_to = key($arr);
$last_value = array_values($arr)[0];
$result = [];
foreach ($arr as $key => $value) {
if ($last_value == $value) {
$key_to = $key;
} else {
$result[] = $key_from . ' - ' . $key_to . ' ' . (($last_value) ? 'true' : 'false');
$key_from = $key;
$key_to = $key;
$last_value = $value;
}
}
$result[] = $key_from . ' - ' . $key_to . ' ' . (($last_value) ? 'true' : 'false');
print_r($result);
<?php
$arr = [
//...
11 => true,
12 => true,
15 => false,
16 => true,
17 => false,
18 => false
//...
];
function group($arr) {
$out = [];
$v_in = reset($arr);
$k_in = $k_out = key($arr);
while( $k_out !== null) {
$v = next($arr);
$k = key($arr);
if( null !== $k && $v === $v_in) {
$k_out = $k;
continue;
}
$out[] = sprintf('%d - %d %s', $k_in, $k_out, $v_in ? 'true' : 'false');
$k_in = $k_out = $k;
$v_in = $v;
}
return $out;
}
print_r( group($arr));
/* Array
(
[0] => 11 - 12 true
[1] => 15 - 15 false
[2] => 16 - 16 true
[3] => 17 - 18 false
) */