S
S
Sergey2015-03-13 06:49:37
PHP
Sergey, 2015-03-13 06:49:37

How, when the end of the array is reached, continue to bypass it from the beginning?

Hello!
There is such an array:

$arrya = array(
    	array(),
  array(),
    	array(),
    	array(),
  // ещё сколько-то раз это же
  	array(),
);

It is necessary to make it so that every day 5 elements of the array are displayed in order, and if there are not enough elements in the array for the whole month, then display the elements again in order from scratch.

Example: There are 75 elements in the array, and the month consists of 30 days, then
- on the first day the elements of the array from 0 to 4
will be displayed - on the second day the elements from 5 to 9 will be displayed
- on the third day from 10 to 14 the key

But on 30 days, 75 elements will not be enough, because it is necessary to display 5 elements per day, and in a month we have 30 days (see the above condition). Therefore, you need to run it again from the beginning:
- on the 14th day 70-74 elements will fall out
- on the 15th day 75-79 elements will fall out

Accordingly, given that there are 75 elements in the array, we will not have enough elements for 15, 16 and other days. Therefore, they must again go around the circle from scratch. That is, like this (the whole cycle started again):

- On day 15: 75, 0, 1, 3, 4
- On day 16: 5-9
- On day 17: 10-14
- On day 18: 15-19

Please help, I've been struggling with this problem for three days now :(
// Число элементов в массиве
$count_array = count($array);

// Выводить элементов на страницу
$count_str = 5;

if($count_array > 0) {
  
  $html = '';
  
  // Текущий день месяца числом
  $j = date('j');
  
  // Конечная позиция
  $end_key = $j * $count_str;
  
  // Начальная позиция
  $start_key = $end_key - $count_str;
  
  for($i = $start_key; $i < $end_key; $i++) {
  
    if(array_key_exists($i, $array)) {
      $value = $array[$i];
      $html .= $i.' => TRUE <br/>';
    }
    else {
      $html .= $i.' => FALSE <br/>';
    }
  }
  
  echo $html;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanatPHP, 2015-03-13
@froops

Your problem is that your offset is not attached to anything. considering that it will be different at the beginning of each month, it is not surprising that you are confused.
If I were you, I would look at the GregorianToJD() function.
Zahardkodil in the script the value returned to it on the day the impressions start
And then calculate the offset from the difference with the current value.
The % operator might come in handy for this.
Well, add array_slice, for beauty

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question