Most Referenced Functions
  » google_pagerank()
  » preg_replace()
  » imagecreatefrompng()
  » site_pageranks()
  » imagepng()
  » imagedestroy()
  » imagestring()
  » imagecolorallocate()
  » htmlentities()
  » fopen()
  » preg_match()
  » header()
  » getimagesize()
  » htmlspecialchars()
  » ob_start()
  » session_start()
  » strstr()
  » ob_flush()
  » preg_match_all()
  » strpos()
  » flush()
  » setcookie()
  » str_replace()
  » array2vars()
  » nl2br()
  » preg_split()
  » ereg()
  » urlencode()
  » ereg_replace()
  » readgzfile()

Become a sponsor for $15/month. Link is sitewide - PR5 homepage, 20+ PR4 pages, 90+ PR3 pages. Email dave[AT]icemelon[D0T]c0m.

PHP Functions

Function: array2vars
By dave

Note! This function is not a built-in PHP function. To use it, you will need to copy the source code (located at the bottom of this page) into your script.

array2vars -- Converts an array into a set of variables, where the keys become the variables' names.

Description

void array2vars ( array KeysWithValues )

This function can be useful in converting your $_POST values into distinct variables. This way, you can just type $keyName instead of $_POST['keyName']. Other similar arrays include $_GET, $_REQUEST, $_SESSION, $_COOKIE; and an array (or row) fetched from your mySQL database, as exemplified in example 2.

Note! It was pointed out by a visitor that the function extract (see related functions) does the exact same thing.

Example 1. array2vars() example

<?php
$icemelon = Array('ice' => 'melon', 'foo' => 'fighters');
array2vars($icemelon);
echo "$ice, $foo "; // outputs: melon, fighters
?>

Example 2.

<?php
$q = mysql_query($query);
while($row = mysql_fetch_array($q)) {
    array2vars($row);
    // use variables
    // ....
}
?>

Source Code

<?php
function array2vars($arr) {
    foreach($arr AS $key=>$var) {
        global $$key;
        $$key = $var;
    }
}
?>

Related Function(s)

  • extract()
  • print_r()
  • array_keys()
  • array_vals()
  • var_dump()
  • Icemelon -- PHP, CSS, Javascript Tutorials, & More!
      © 2005-2010 Icemelon.com   Email: dave[AT]icemelon[D0T]c0m