PHP checks if the specified key name exists in the array of functions array_key_exists ()

Source: Internet
Author: User

Instance

Check to see if the key name "Volvo" exists in the array:

<?php$a=array ("Volvo" = "XC90", "BMW" = "X5"), if (Array_key_exists ("Volvo", $a)) {echo "Key exists!";} Else{echo "Key does not exist!";}? >

Definition and usage

The array_key_exists () function checks to see if the specified key name exists in an array, returns true if the key name exists, or False if the key name does not exist.

Tip: Keep in mind that if you omit the key name when you specify an array, an integer key name that starts at 0 and increments by 1 is generated. (see Example 2)

Grammar

Array_key_exists (Key,array)
Parameters Describe
Key Necessary. Specifies the key name.
Array Necessary. Specifies the array.

Technical details

return value: Returns TRUE if the key name exists, or FALSE if the key name does not exist.
PHP version: 4.0.7+

More examples

Example 1

Check that the key name "Toyota" is present in the array:

<?php$a=array ("Volvo" = "XC90", "BMW" = "X5"), if (Key_exists ("Toyota", $a)) {echo "Key exists!";} Else{echo "Key does not exist!";}? >

Example 2

Check that the integer key name "0" is present in the array:

<?php$a=array ("Volvo", "BMW"), if (array_key_exists (0, $a)) {echo "Key exists!";} Else{echo "Key does not exist!";}? >

Example 1

<?php $a =array ("a" and "Dog", "b" = "Cat"); if (Array_key_exists ("a", $a)) {echo "Key exists!";} else {echo "key does not exist!";}?>

Output:

Key exists!

Example 2

<?php $a =array ("a" and "Dog", "b" = "Cat"); if (Array_key_exists ("C", $a)) {echo "Key exists!";} else {echo "key does not exist!";}?>

Output:

Key does not exist!

Example 2

<?php $a =array ("Dog", Cat "); if (array_key_exists (0, $a)) {echo "Key exists!";} else {echo "key does not exist!";}?>

Output:

Key exists!

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.