array key exists function in php - checks if key or index exists in the array

php example code : array_key_exists function in php - checks if key or index exists in the array

array-key-exists-example-in-php.php


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title>php example code : array_key_exists function in php - checks if key or index exists in the array</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="Author" content="Md Iqbal Hosan">
  <meta name="title" content="php example code : array_key_exists function in php - checks if key or index exists in the array">
  <meta name="keywords" content="php, example, code, array, function, array_key_exists">
  <meta name="description" content="php example code : array_key_exists function in php - checks if key or index exists in the array">
 </head>
 <body>
  <h3>php example code : array_key_exists function in php - checks if key or index exists in the array</h3>
  <?php
   /*
    ---------------------------------------------------------------------
    let us make an array with some keys and values
    ---------------------------------------------------------------------
   */
   $myArray = array("key1"=>"value1","key2"=>"value2","key3"=>"value3");
   echo "<p style='background:#ddd; color:red;'>Output of my array</p>";
   echo "<pre>";
   print_r($myArray);
   echo "</pre>";
   
   /*
    ---------------------------------------------------------------------
    let us make a key to test in the array, whether it is exist or not
    ---------------------------------------------------------------------
   */
   $myKey = "key2";
   /*
    ---------------------------------------------------------------------
    check whether the key is exist or not in myArray
    ---------------------------------------------------------------------
   */
   $keyExistanceChecker = array_key_exists($myKey, $myArray);

   echo "<p style='background:#ccc; color:red;'>Output of keyExistanceChecker using array_key_exists function</p>";
   echo "<pre>";
   if($keyExistanceChecker == true)
   {
    echo "{$myKey} : is exist in the array."; 
   }
   else
   {
    echo "{$myKey} : is not exist in the array."; 
   }
   echo "</pre>";
  ?>
 </body>
</html>


php example code : array_key_exists function in php - checks if key or index exists in the array - output in the browser

No comments:

Post a Comment

leave your comments here..