如何计算PHP中in_array值的数量?

时间:2017-12-25 13:08:11

标签: php mysql

我正在使用PHP mysqli数据库。我在数据库中检索一些数据并在in_array条件下使用它。我的PHP代码是:

total database memberno(0001,0002,0003,0004,0005,0006,0007,0008 to 0016)

$relation_ship_no_findempty = '00010001';
$myid = '1';  
$select_query_findempty = "select * from member_tree where relation_ship_no LIKE '%{$relation_ship_no_findempty}%' AND tree_id > '$myid' AND x_position = '2'";
$run_findempty = mysqli_query($con,$select_query_findempty);

while ($row_findempty = mysqli_fetch_array($run_findempty)) {
    $member_no = $row_findempty[ 'member_no']; 
    $arr1 = array('0005','0016', '0010', '0013', '0006');
    if (!in_array($member_no, $arr1)) {
        $member_no."<br>";    
    }  
}

出现的输出:

0001
0002
0003

我的预期输出是:

3 

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

这将返回行数:

$run_findempty = mysqli_query($con,$select_query_findempty);
$num_rows = mysqli_num_rows($run_findempty);

官方文件:

http://php.net/manual/en/mysqli-result.num-rows.php

答案 1 :(得分:0)

 $arr1 = array('0005','0016', '0010', '0013', '0006');

你可以在查询中过滤使用implode和count

$select_query_findempty = "select * from member_tree where relation_ship_no LIKE '%{$relation_ship_no_findempty}%' AND tree_id > '$myid' AND x_position = '2' and member_no not in (".implode(',', $arr1).")";

$count = mysqli_num_rows($run_findempty);