Coding Villa

Check key exist or not in array using array_key_exists in PHP

Author : Tahreem Anwar

Share with Friends
Views : 406
Ratings : 0
Bookmarks : 0
Farourities : 0

Description


In this simple article you will learn about Check key exist or not in array using array_key_exists in PHP.


Advertisements


 

array_key_exists: 

 

 

Code:

 

1.    <?php

2.    $names=array("t"=>"tahreem","r"=>"rizwan","a"=>"Anwar");

3.    if (array_key_exists("t",$names))

4.    {

5.    echo "Key Exists in that array";

6.    }

7.    else

8.    {

9.    echo "Does not exist that key!!!";

10.  }

11.  ?>

 

 

Output:

 

Key Exists in that array

 

Explanation:

 

In the above code on line 2 we have declared an Associative array where each ID key is associated with a value. On line 3 we have passed that array to array_key_exits function that take two parameter first is the index name and second the array name that is to be search for index. On line 3 we have passed that function to check if index name exits in the array it returns true and print Key Exists in that array else print Does not exist that key!!!.

 

Code:

 

1.    <?php

2.    $names=array("t"=>"tahreem","r"=>"rizwan","a"=>"Anwar");

3.    if (array_key_exists(1,$names))

4.    {

5.    echo "Key Exists in that array";

6.    }

7.    else

8.    {

9.    echo "Does not exists that key!!!";

10.  }

11.  ?>

 

Output:

 

Does not exists that key!!!

  

Explanation:

 

One line 3 we have passed array_key_exits function that checks if index exists or does not exist in $names array. So the output is Does not exists that key.

 



Article Tags
Share with Friends

Comments




Leave a Reply

Name (required)  
Mail (will not be published) (required)   
 
Enter This Code
Captcha
 

Advertisements

Article Categories

.NET


Databases


Mobile Development


Operating Systems


Web Development


Coding Villa on Facebook