Code example:

$list = array( array('name' => 'test'), array('name' => ''), array('name' => 'test2') ); echo 'В этом списке N элементов:<br>'; $i = 0; foreach ($list as $el){ if(empty($el['name'])) continue; echo ++$i.') '.$el['name'].'<br>'; } echo 'Вот это значение нужно вывести в место "N" до списка: '.$i; 

Result:

enter image description here

How to correctly display this value before it is even calculated?
Just prepare the array in advance, sorting it out and output the array length (will you have to iterate over the array twice) or are there more correct solutions?

  • You can try not to display the values ​​inside the loop, but to collect a string from them. - rjhdby pm

1 answer 1

This is the place to be, but better still use the MVC or MVVC pattern. And prepare the data not during the output, but before they are output!