PHP isset 和 array_key_exists 对比

   经常使用 isset 判断变量或数组中的键是否存在,但是数组中可以使用 array_key_exists 这个函数,那么这两个

哪一个更优呢?

   官方文档这样定义两者:

   isset:语言构造器,用于检测变量是否已设置并且非 NULL,文档地址:http://php.net/manual/zh/function.isset.php

   array_key_exists:函数,用于检查数组里是否有指定的键名或索引,文档地址:http://php.net/manual/zh/function.array-key-exists.php

   注意:

    1.isset 对于数组中为 NULL 的值不会返回 true,而 array_key_exists() 会

    2.array_key_exists() 仅仅搜索第一维数组的键。多维数组里嵌套的键不会被搜到

      接下来我们来对比一下两者的执行效率,测试代码如下:

      #Example php version 5.2.5

<?php
  2 
  3 $test_arr = array('aa'=>'dd','bb'=>'','cc'=>null,'dd'=>false);
  4 
  5 echo "isset aa is ";var_dump(isset($test_arr['aa']));echo "
";
  6 echo "isset bb is ";var_dump(isset($test_arr['bb']));echo "
";
  7 echo "isset cc is ";var_dump(isset($test_arr['cc']));echo "
";
  8 echo "isset dd is ";var_dump(isset($test_arr['dd']));echo "
";
  9 
 10 //############################
 11 
 12 echo "key_exist aa is ";var_dump(array_key_exists('aa',$test_arr));echo "
";
 13 echo "key_exist bb is ";var_dump(array_key_exists('bb',$test_arr));echo "
";
 14 echo "key_exist cc is ";var_dump(array_key_exists('cc',$test_arr));echo "
";
 15 echo "key_exist dd is ";var_dump(array_key_exists('dd',$test_arr));echo "
";
 16 echo "key_exist none is ";var_dump(array_key_exists('none',$test_arr));echo "
";
 17 
 18 //#############################
 19 
 20 $start_time = microtime(true);
 21 for($i = 0;$i < 10000;$i++) {
 22     isset($test_arr['aa']);
 23 }
 24 $end_time = microtime(true);
 25 $time = $end_time - $start_time;
 26 echo "isset 10000 is $time
";
 27 
 28 $start_time = microtime(true);
 29 for($i = 0;$i < 1000000;$i++) {
 30       isset($test_arr['aa']);
 31 }
 32 $end_time = microtime(true);
 33 $time = $end_time - $start_time;
 34 echo "isset 1000000 is $time
";
 35 
 36 //##############################
 37 
 38 $start_time = microtime(true);
 39 for($i = 0;$i < 10000;$i++) {
 40    array_key_exists('aa',$test_arr);
 41 }
 42 $end_time = microtime(true);
 43 $time = $end_time - $start_time;
 44 echo "array_key_exists 10000 is $time
";
 45 
 46 $start_time = microtime(true);
 47 for($i = 0;$i < 1000000;$i++) {
 48     array_key_exists('aa',$test_arr);
 49  }
 50  $end_time = microtime(true);
 51  $time = $end_time - $start_time;
 52  echo "array_key_exists 1000000 is $time
";
 53 
 54 //###############################
 55 
 56 $start_time = microtime(true);
 57 for($i = 0;$i < 10000;$i ++) {
 58   in_array('dd',$test_arr);
 59 }
 60 $end_time = microtime(true);
 61 $time = $end_time - $start_time;
 62 echo "in_array 10000 is $time
";
 63 
 64 $start_time = microtime(true);
 65 for($i = 0;$i < 1000000;$i++) {
 66   in_array('dd',$test_arr);
 67 }
 68 $end_time = microtime(true);
 69 $time = $end_time - $start_time;
 70 echo "in_array 1000000 is $time
";

-----------------------------------------------

输出结果

isset aa is bool(true)

isset bb is bool(true)

isset cc is bool(false)

isset dd is bool(true)

key_exist aa is bool(true)

key_exist bb is bool(true)

key_exist cc is bool(true)

key_exist dd is bool(true)

key_exist none is bool(false)

isset 10000 is 0.00150918960571
isset 1000000 is 0.113389968872
array_key_exists 10000 is 0.00272583961487
array_key_exists 1000000 is 0.272269964218
in_array 10000 is 0.00281500816345
in_array 1000000 is 0.281803131104

         从上面的示例中我们可以看出 isset() 对应数组的 value 为 null 会返回 false,当对应数组为 空字符串或false 的时候

返回 true

         array_key_exists() 只有数组不存在对应的 key 时才返回 false,其余返回 true

         另外,从上面示例中也可以看出执行效率 isset > array_key_exists > in_array 

原文地址:https://www.cnblogs.com/leeyongbard/p/10284551.html