CopyPastehas never been so tasty!

Check key exist or not in array using array_key_exists in PH

by anonymous

  • 0
  • 0
  • 0
74 views

http://codingvilla.com/check-key-exist-php-single-article-680.aspx

 

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 toarray_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.

 

Add A Comment: