Zend - The PHP Company




Arrays

Add Code


Assign From Array (v2.0)  

Type: code fragment
Added by: liquidkernel
Entered: 13/09/2002
Last modified: 09/12/2001
Rating: - (fewer than 3 votes)
Views: 4507
Runs through an array, assigns variables based on key name. Ability to append or prepend text to newly created variables. Useful for adapting current applications that depend on register_globals to work without it.


<?php
function assignFromArray($array$prepend=""$append="") {
    foreach (
$array as $key => $value) {
    global ${
$prepend $key $append};
    ${
$prepend $key $append} = $array[$key];
    }
}
?>


Usage Example


$arrTest['test_value'] = "Testing";

/* Default function useage */
assignFromArray($arrTest);

echo $test_value; // will print "Testing"

/* Prepending and appending to variables */
assignFromArray($arrTest, "a_", "_1");

echo $a_test_value_1; // will also print "Testing"


Rate This Script





Search



This Category All Categories