There is a news page, It turns out I have a pagination, I display 18 pcs. on every page

0, 1, 2 3, 4, 5 6, 7, 8 9, 10, 11 12, 13, 14, 15, 16, 17 

I need to get from this array 0, 5, 6, 11, 12, 17

I tried using the for loop to select 6 pieces of the first and the last element, but I couldn’t implement it to the end

Closed due to the fact that the essence of the question is not clear to the participants of Dmitriy Simushev , cheops , VenZell , Grundy , aleksandr barakin 12 May '16 at 8:46 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • In what sense to "get it"? What have you tried to do to solve the problem and what exactly did not work out? - Dmitriy Simushev
  • @DmitriySimushev corrected - gudfar
  • Well, it would be nice to formalize the logic of obtaining the ID explicitly - Dmitriy Simushev
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

2 answers 2

Add the variable "missing pages". In the loop:
1. If this variable is less than the desired value (4?) Then skip the output, add to variable 1.
2. If this variable> = 4 then reset the variable, display the page.
I hope it is clear, there is no possibility now to throw the code ...

  • Thank you friend! Helped - gudfar

I would do as follows:

 $newArray = []; $array = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, ]; $iter= new ArrayIterator($array); $seek = 0; $count = $iter->count(); $id = $iter->current(); $newArray[] = $id; $seek += 5; do { $iter->seek($seek); $id = $iter->current(); $newArray[] = $id; $seek += 1; if($seek >= $count) { break; } $iter->seek($seek); $id = $iter->current(); $newArray[] = $id; $seek += 5; }while($seek < $count); var_dump($newArray); 

A simplified example:

 $newArray = []; $array = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, ]; $count = count($array); $seek = 0; if(isset($array[$seek])) { $newArray[] = $array[$seek]; } while($seek < $count) { $seek += 5; if(isset($array[$seek])) { $newArray[] = $array[$seek]; } $seek += 1; if(isset($array[$seek])) { $newArray[] = $array[$seek]; } } var_dump($newArray); 
  • And you can even overcompass? The practical limit is interesting ... - Caravus
  • Well then write a super optimized example of you. In general, this is just an example of a solution, and not an "ideal" solution ... Added a variant to the comment without using an iterator. That's better? - Maxim Tkach