Home  >  Article  >  Backend Development  >  PHP array function array_key_exists() finds whether the array key exists

PHP array function array_key_exists() finds whether the array key exists

WBOY
WBOYOriginal
2016-07-25 09:04:501087browse
  1. $a=array("a"=>"Dog","b"=>"Cat");
  2. if (array_key_exists("a",$a))
  3. {
  4. echo "Key exists!";
  5. }
  6. else
  7. {
  8. echo "Key does not exist!";
  9. }
  10. ?>
Copy code

Output: Key exists!

Example 2:

  1. $a=array("a"=>"Dog","b"=>"Cat");
  2. if (array_key_exists("c",$a))
  3. {
  4. echo "Key exists!";
  5. }
  6. else
  7. {
  8. echo "Key does not exist!";
  9. }
  10. ?>
Copy code

Output: Key does not exist!

Example 3:

  1. $a=array("Dog",Cat");
  2. if (array_key_exists(0,$a))
  3. {
  4. echo "Key exists!";
  5. }
  6. else
  7. {
  8. echo "Key does not exist!";
  9. }
  10. ?>
Copy code

output: Key exists!



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn