Rumah > pembangunan bahagian belakang > tutorial php > php数组函数in_array() 查找数组值是否存在

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Lepaskan: 2016-07-25 09:12:47
asal
980 orang telah melayarinya

在php编程中,in_array() 函数在数组中搜索给定的值。

in_array() 定义和用法 in_array() 函数在数组中搜索给定的值。

语法 in_array(value,array,type) 参数 描述 value 必需。规定要在数组搜索的值。 array 必需。规定要搜索的数组。 type 可选。如果设置该参数为 true,则检查搜索的数据与数组的值的类型是否相同。

说明 如果给定的值 value 存在于数组 array 中则返回 true。如果第三个参数设置为 true,函数只有在元素存在于数组中且数据类型与给定值相同时才返回 true。

如果没有在数组中找到参数,函数返回 false。

注释:如果 value 参数是字符串,且 type 参数设置为 true,则搜索区分大小写。

例1,in_array实例。

  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,in_array实例。

  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



Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan