douhaodang0403 2017-11-10 09:45
浏览 28
已采纳

检查php数组是否包含值[重复]

This question already has an answer here:

i have an array $captions and i want to check if there is at least one value inside or not. the array could looks like that:

Array
(
    [0] => 84
    [1] => 
    [2] => 82
    [3] => 
    [4] => 
    [5] => 
    [6] => 
    [7] => 
    [8] => 
    [9] => 
)

and my way to check if there is "more than 0", "more than 1"...

if (count($captions) > 0)   { echo '<br>bigger than 0';}
if (count($captions) === 1) {echo '<br>is 1';};
if (count($captions) > 1)   {echo '<br>gbigger than 1';};
if (count($captions) > 2)   {echo '<br>bigger than 2';}

but: it gives me with this array above follwing result:

bigger than 0
bigger than 1
bigger than 2

"bigger than 2" should not be, because the array contains only two values? what am i getting wrong?

</div>
  • 写回答

3条回答 默认 最新

  • douya1855 2017-11-10 09:48
    关注

    You need to do like this (using array_filter()):-

    echo  "equal to" . count(array_filter($array));
    

    Output:- https://eval.in/897055 Or https://eval.in/897063

    Note:- In your code you are counting empty values too, hense ambiguity occur. You need to first remove empty values and then need to count.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类