A possible use for extract() is to import into the symbol table variables contained in an associative array returned by
<?php
/* Suppose that $var_array is an array returned from
wddx_deserialize */
$size = "large";$var_array = array("color" => "blue",
"size" => "medium",
"shape" => "sphere");extract($var_array, EXTR_PREFIX_SAME, "wddx");
echo "$color, $size, $shape, $wddx_size\n";
?>
---------------------------------------------------------
compact — Create array containing variables and their values
<?php
/* Suppose that $var_array is an array returned from
wddx_deserialize */
$size = "large";$var_array = array("color" => "blue",
"size" => "medium",
"shape" => "sphere");extract($var_array, EXTR_PREFIX_SAME, "wddx");
echo "$color, $size, $shape, $wddx_size\n";
?>
The above example will output:
blue, large, sphere, medium
compact — Create array containing variables and their values
Example #1 compact() example
<?php
$city = "San Francisco";$state = "CA";$event = "SIGGRAPH";
$location_vars = array("city", "state");
$result = compact("event", "nothing_here", $location_vars);print_r($result);?>
The above example will output:
Array ( [event] => SIGGRAPH [city] => San Francisco [state] => CA )
No comments:
Post a Comment