计算Foreach循环中的特定数字

时间:2019-03-31 20:20:16

标签: php loops foreach count

特定数字在foreach循环中出现多少次。

为此,我尝试使用array_count_values函数,但在foreach循环中返回具有上述值的合计。

<table>
  <tr>
    <th>User ID</th>
    <th>Number</th>
  </tr>
  <?php foreach($users as $user_id){ ?>
  <tr>
     <td><?php echo $id = $user_id->ID; ?></td>

  <?php $user_results = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM " . $wpdb->prefix . "mlw_results WHERE deleted='0' AND  (user=%d OR email='%s')", $id, $email) ); ?>

   <?php foreach( $user_results as $result ) { ?>
      <td><?php echo  $result->correct_score.', ';?></td>
   <?php } ?>

  </tr>  
<?php } ?>
</table>

当前输出:-

   UserID                 Number

        1                 33, 55, 88, 100, 66, 4

        2                 33, 77, 88, 100, 100, 4

        3                 22, 51, 80, 100, 100, 4

        4                 1, 48, 56, 15, 78, 65, 45  

预期输出:-

   UserID                 Number

       1                  100 comes 1 time.

       2                  100 comes 2 time.

       3                  100 comes 2 time.

       4                  100 comes 0 time.

4 个答案:

答案 0 :(得分:0)

如果我了解您的数组和foreach,那么您应该能够使用array_count_values,因为它会返回一个关联数组。

将第二个foreach替换为:

$counts = array_count_values($user_results->correct_score);
echo "100 comes ";
if(isset($counts[100])){
    echo $counts[100];
}else{
    echo "0";
}
echo " times";

答案 1 :(得分:0)

输入一个计数器,如果等于100,则放置if,例如:

<table>
  <tr>
    <th>User ID</th>
    <th>Number</th>
  </tr>
  <?php foreach($users as $user_id){ ?>
    <tr>
       <td><?php echo $id = $user_id->ID; ?></td>

      <?php

        $user_results = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM " . $wpdb->prefix . "mlw_results WHERE deleted='0' AND  (user=%d OR email='%s')", $id, $email) );

        $count=0;

        foreach( $user_results as $result ) { 
          if($result->correct_score===100){
            $count++;
          }
        }
      ?>
      
      <td><?php echo '100 comes '.$count.' time.';?></td>
    </tr>  
  <?php } ?>
</table>

答案 2 :(得分:0)

  

代码:

<?php

$data = [
    1 => [33, 55, 88, 100, 66, 4],
    2 => [33, 77, 88, 100, 100, 4],
    3 => [22, 51, 80, 100, 100, 4],
    4 => [1, 48, 56, 15, 78, 65, 45]
];


function printOccurence(array $data, $neddle)
{
    foreach ($data as $key => $dat) {

        if (in_array($neddle, $dat)) {
            echo "$key comes $neddle " . array_count_values($dat)[$neddle] . " times." . PHP_EOL;

        } else {
            echo "$key comes $neddle " . "0" . " times." . PHP_EOL;
        }
    }
}

printOccurence($data, 100);
  

用法:

printOccurence($data, 100);
  

结果:

1 comes 100 1 times.
2 comes 100 2 times.
3 comes 100 2 times.
4 comes 100 0 times.

答案 3 :(得分:0)

我这样解决https://3v4l.org/v70jC

$numbers = [
    [33, 55, 88, 100, 66 ,4],
    [33, 77, 88, 100, 100, 4],
    [22, 51, 80, 100, 100, 4],
    [1, 48, 56, 15, 78, 65, 45 ]
];

foreach ($numbers as $row) {
    if (in_array(100, $row)) {
        $amountOfHundreds = array_count_values($row)[100];
        echo $amountOfHundreds . PHP_EOL;
    }
}