array compact function - creates array containing variables as keys and their values as the values of the respective variables

array compact function - creates array containing variables as keys and their values as the values of the respective variables

compact-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>compact function - creates array containing variables as keys and their values as the values of the respective variables</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="Author" content="Md Iqbal Hosan">
  <meta name="title" content="compact function - creates array containing variables as keys and their values as the values of the respective variables">
  <meta name="keywords" content="php, example, code, array, function, compact">
  <meta name="description" content="compact function - creates array containing variables as keys and their values as the values of the respective variables">
 </head>
 <body>
  <h3>compact function - creates array containing variables as keys and their values as the values of the variables respectively</h3>
  <?php
   /*
    -----------------------------------------------------------
    let us make some variables and fill up them with values
    -----------------------------------------------------------
   */
   $visitorName = "cfsuman";
   $visitorAddress = "dhaka";
   $visitorAge = 30;
   $visitorStatus = "active";
   $myArray = compact("visitorName","visitorAddress","visitorAge","visitorStatus");
   echo "<p style='background:#aaa; color:red;'>Output of myArray</p>";
   echo "<pre>";
   print_r($myArray);
   echo "</pre>";
   /*
    -------------------------------------------------------------------------------------
    here compact() takes some variables as input
    creates an array where keys are variables name and there values as values
    -------------------------------------------------------------------------------------
   */
   /*
    -------------------------------------------------------------------------------------
    lets make it another way by the help of array
    -------------------------------------------------------------------------------------
   */
   $compactFeederArray = array("visitorName","visitorAddress","visitorAge","visitorStatus");
   echo "<p style='background:#bbb; color:red;'>Output of compactFeederArray</p>";
   echo "<pre>";
   print_r($compactFeederArray);
   echo "</pre>";
   /*
    -------------------------------------------------------------------------------------
    now final array using compact() function
    -------------------------------------------------------------------------------------
   */
   $finalArray = compact($compactFeederArray);
   echo "<p style='background:#ccc; color:red;'>Output of finalArray by using compact() function</p>";
   echo "<pre>";
   print_r($finalArray);
   echo "</pre>";
  ?>
 </body>
</html>

array compact function - creates array containing variables as keys and their values as the values of the respective variables - output in the browser

No comments:

Post a Comment

leave your comments here..