Zend - The PHP Company




Miscellaneous

Add Code


Assign Variables from Array  

Type: code fragment
Added by: liquidkernel
Entered: 08/05/2002
Last modified: 05/12/2001
Rating: - (fewer than 3 votes)
Views: 4566
This is a function I wrote that runs through a single dimensional array and assigns variables based on the key names.


<?php
function assign_from_array($array) {
    
$keys array_keys($array);
    for (
$i=0$i<count($array); $i++) {
        
$key $keys[$i];
    global ${
$key};
    ${
$key} = $array[$key];
    }
}
?>


Usage Example


Example if external variables are not global in PHP. Variable "option" with value of "1" is passed to the script via the POST method.

echo $HTTP_POST_VARS['option'] // prints "1"
echo $option; // prints nothing
assign_from_array($HTTP_POST_VARS);
echo $option; // prints "1"





Rate This Script





Search



This Category All Categories