array dif key function in php computes the difference of arrays using keys

php example code : array_diff_key function in php - computes the difference of arrays using keys

array-diff-key-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>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <title>php example code : array_diff_key function in php - computes the difference of arrays using keys</title>
 </head>
 <body>
  <h3>php example code : array_diff_key function in php - computes the difference of arrays using keys</h3>
  <?php
   /*
    --------------------------------------------------
    let us make an array, with some keys and values
    --------------------------------------------------
   */
   $myFirstArray = array('key1'=>10, 'key2'=>11, 'key3', 5);
   echo "<p>myFirstArray in the browser with structure</p>";
   echo "<pre>";
   print_r($myFirstArray);
   echo "</pre>";

   /*
    -----------------------------------------------------
    let us make another array, with some keys and values
    -----------------------------------------------------
   */
   $mySecondArray = array('key1'=>10, 'key2'=>12, 'key3'=>2, 5);
   echo "<p>mySecondArray in the browser with structure</p>";
   echo "<pre>";
   print_r($mySecondArray);
   echo "</pre>";
   
   $finalArray = array_diff_key($myFirstArray, $mySecondArray);
   echo "<p style='background:#eee; color:red;'>Output of final array using array_diff_key function</p>";
   echo "<pre>";
   print_r($finalArray);
   echo "</pre>";
   /*
    -----------------------------------------------------------------
    [1] => 5, because key [1] is not common and exist in first array
    -----------------------------------------------------------------
   */
  ?>
 </body>
</html>

php example code : array_diff_key function in php - computes the difference of arrays using keys - output in the browser

No comments:

Post a Comment

leave your comments here..