Dear programmers, please help a newbie.

In the array I indicated people who are taller than Ira in her class, her height is 169, chose these people and brought them to the screen, and now I’m wondering how to calculate and display the amount on the monitor? Help...

code example:

<?php $Ira = 169; $class = array ( 'anton' => 172, 'igor' => 173, 'dasha' => 188, 'dima' => 160 ); foreach ($class as $name => $hai) { if (in_array($hai>169, $class)) { echo $name,'<br/>'; } } 

    2 answers 2

    You can try to make it easier:

    Use array_filter to sort people by a given criterion. And then just take the count resulting array

     // input data $Ira = 169; $class = array ( 'anton' => 172, 'igor' => 173, 'dasha' => 188, 'dima' => 160 ); // logic $people = array_filter($class, function($class) use ($Ira) { return $class > $Ira; }); echo count($people); // выведет 3 

      In the condition you need to summarize, and after the cycle output

       $class = array ( 'anton' => 172, 'igor' => 173, 'dasha' => 188, 'dima' => 160 ); $sum = 0; foreach ($class as $name => $hai) { if (in_array($hai>169, $class)) { echo $name,'<br/>'; $sum++; } } echo $sum; 
      • It seems that the amount of growth does not mean - Naumov
      • Sorry, the number of people who had a higher mind ... - Vika Smirnova
      • @KBMira, well, corrected - Komdosh
      • @Komdosh Thank you for the answer, but could you explain $ __ ++ in a loop always gives you a sum? - Vika Smirnova
      • No, $ sum is a normal variable, before the cycle I set it to 0, and then at each step of the cycle, when the condition was triggered, it increased $ sum by one. $ sum ++ is a short analogue of sum = $ sum + 1; - Komdosh