Question: I have an array of numbers, for example (calendar days):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

I want to highlight every three numbers after two.

So it should looks like: **1** **2** **3** 4 5 **6** **7** **8** 9 10 **11** **12** **13** 14 15 **16** **17** **18** 19 20 **21** **22** **23** 24 25 **26** **27** **28** 29 30 **31**

Or it can be two after two, or four after two or any other pair.

I need some algorithm to make this work, help please.

Answer: 
You can use modulo `%`. If modulo is less than or equal to 2 (0,1,2) highlight it.

```
$arr = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31);

    foreach( $arr as $key => $value ) {
        if ( ( $key % 5 ) <= 2 ) echo "<b>" . $value  . "</b>"; 
        else echo $value; 

        echo "<br />";
    }

```
Here is one method using [array\_slice](http://php.net/manual/en/function.array-slice.php) and [implode](http://php.net/manual/en/function.implode.php) to build the string. 

This does fewer loops. How many loops depends on the "settings".  

In this case you mention it makes seven loops compared to 31 when you loop every value of the array. 

```
$days = range(1,31);
$i = 3; // consecutive bolded days
$j = 2; // consecutive not bolded days between the bolded
$str ="";
for($k=0;$k<end($days);){
    $str .= "<b>" . implode("</b> <b>", array_slice($days, $k,$i)) ."</b> " . implode(" ", array_slice($days, $k+$i, $j)). " ";
    $k=$k+$i+$j;
}
echo $str;

```

<https://3v4l.org/CpoVb> 

Array\_slice captures first the values that should be bolded, then captures the ones that should not be bolded until the next "bold" value and stores it in $str.