PHP array_key_exists () array functions using an explanation

Source: Internet
Author: User
This article gives you a summary of the PHP array function array_key_exists () knowledge and performance comparison with In_array, and the isset of the difference analysis, very detailed, the need for small partners can refer to.

The array_key_exists () function determines whether the specified key exists in an array, returns true if key exists, or returns flase

Array_key_exists (Key,array);

Key: Required. Specified key Name
Array: Required. Specify an array of inputs

<?php$a = Array (' a ' = ' = ' Dog ', ' b ' = ' = ' Cat '), if (array_key_exists (' a ', $a)) {  echo ' key exists! ';} else{  Echo ' Key does not exist! ';}? >

Output: Key exists!

Why is array_key_exists faster than In_array?

Array_key_exists and In_array are not the same thing.
Array_key_exists determine if there is a key value
Array_key_exists (A,arr)->if (Isset (Arr[a])) is true

And the In_array needs to traverse the value to traverse it before jumping out of the loop

Ask:
is the index of the array has a separate storage unit, and optimized, array_key_exists time complexity is O (1), and In_array is O (n)??

Chase Answer:
In terms of complexity.

Array_key_exists is to determine if a key has a value

In_array to traverse once to get whether the same does not know the built case must traverse

The difference between isset and array_key_exists in PHP

1. For the value of the array is different, for a value of NULL or ' or false,isset return false,array_key_exists true;

2. Execution efficiency is different, isset is built-in operator, Array_key_exists is PHP built-in function, isset to be faster. Please refer to: PHP function implementation principle and performance analysis

3. When using Isset to access a non-existent index array value, it will not cause a e_notice PHP error message;

4.array_key_exists will call Get_defined_vars to determine if the array variable exists, isset not;

Test code:

<?phpfunctionmicrotime_float () {list ($usec, $sec) = Explode ("", Microtime ()), return (float) $usec + (float) $sec);} $test _arr[' AA ']= ' dd '; $test _arr[' BB ']= '; $test _arr[' cc ']=null; $test _arr[' DD ']=false; $test _arr= Array (' aa ' = ' = ') DD ', ' bb ' = ', ' cc ' =>null, ' DD ' =>false); echo "Isset aa is"; Var_dump (Isset ($test _arr[' AA ')); echo "n"; echo " Isset BB is "; Var_dump (Isset ($test _arr[' BB")), echo "n"; echo "Isset cc is"; Var_dump (Isset ($test _arr[' cc ')); echo "n"; echo "Isset dd is"; Var_dump (Isset ($test _arr[' cc ')), echo "n"; echo "isset none is"; Var_dump (Isset ($test _arr[' none ')); echo "n"; echo "Key_exist aa is"; Var_dump (array_key_exists (' AA ', $test _arr)), echo "n"; echo "Key_exist BB is"; Var_dump ( array_key_exists (' BB ', $test _arr)), echo "n", echo "Key_exist cc is", Var_dump (array_key_exists (' cc ', $test _arr)), echo " n "; echo" key_exist dd is "; Var_dump (array_key_exists (' DD ', $test _arr)); echo" n "; echo" key_exist none "; Var_dump (array _key_exists (' None ', $test _arr)); echo "n"; $time _start = Microtime_float(); for ($i =0; $i <100; $i + +) {isset ($test _arr[' AA ');} $time _end = microtime_float () $time = $time _end-$time _start;echo "isset" $timen "; for ($i =0; $i <10000; $i + +) { Isset ($test _arr[' AA ');} $time _end = microtime_float () $time = $time _end-$time _start;echo "Isset 10000 is $timen", for ($i =0; $i <1000000; $i + +) { Isset ($test _arr[' AA ');} $time _end = microtime_float () $time = $time _end-$time _start;echo "Isset 1000000 is $timen";//+++++++++++++++++++++++++ +++++ $time _start = Microtime_float (); for ($i =0; $i <100; $i + +) {array_key_exists (' AA ', $test _arr);} $time _end = microtime_float () $time = $time _end-$time _start;echo "Array_key_exists $timen"; for ($i =0; $i <10000 ; $i + +) {array_key_exists (' AA ', $test _arr);} $time _end = microtime_float () $time = $time _end-$time _start;echo "array_key_exists 10000 is $timen", for ($i =0; $i < 1000000; $i + +) {array_key_exists (' AA ', $test _arr);} $time _end = microtime_float () $time = $time _end-$time _start;echo "array_key_exists 1000000 is $timen";

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.