欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php数组函数 in_array() 查找数组中是否存在指定值

程序员文章站 2022-04-30 21:03:31
...
  1. $people = array("Peter", "Joe", "Glenn", "Cleveland");

  2. if (in_array("Glenn",$people))

  3. {
  4. echo "Match found";
  5. }
  6. else
  7. {
  8. echo "Match not found";
  9. }
  10. ?>
复制代码

输出: Match found

例2:

  1. $people = array("Peter", "Joe", "Glenn", "Cleveland", 23);

  2. if (in_array("23",$people, TRUE))

  3. {
  4. echo "Match found
    ";
  5. }
  6. else
  7. {
  8. echo "Match not found
    ";
  9. }if (in_array("Glenn",$people, TRUE))
  10. {
  11. echo "Match found
    ";
  12. }
  13. else
  14. {
  15. echo "Match not found
    ";
  16. }if (in_array(23,$people, TRUE))
  17. {
  18. echo "Match found
    ";
  19. }
  20. else
  21. {
  22. echo "Match not found
    ";
  23. }
  24. ?>
复制代码

输出: Match not found Match found Match found