I need that after the limit of values ​​(that is, when more than 4x li), ul was re-deduced, already with the following values ​​li. I did something like this, but of course not correctly .:

for ($i=4; $i <= count($tour_incl); $i+4) { $i -= 1; $u_count=$i-4; for ($j=$u_count; $j < $i; $j++) { echo '<ul class="list-ok" style="margin-top:50px; display:inline-block;">'; echo '<li style="padding-left: 14px;margin-left: 15px;">' . $tour_incl[$j] . '</li>'; echo '</ul>'; } } 
  • $ tour_incl in this case = 8 - Tofiq Şamedov
  • And two cycles why? one is enough, with a single step ... li unconditionally, and ul and /ul depending on the remainder of dividing i by 4 ... - Akina

1 answer 1

You can simply split the array into parts using array_chunk() , and then form a string in the loop, combining the elements of the array using join() :

 $tour_incl = array( 1,2,3,4,5,6,7,8,9,10 ); array_map(function($a){ echo '<ul><li>' . join('</li><li>', $a) . '</li></ul>'; }, array_chunk($tour_incl, 4)); 

Result:

 <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> </ul> <ul> <li>5</li> <li>6</li> <li>7</li> <li>8</li> </ul> <ul> <li>9</li> <li>10</li> </ul> 
  • Thanks, perfect! - Tofiq Şamedov