Thursday, April 1, 2010

extract and compact

extractImport variables into the current symbol table from an array

int extract ( array $var_array [, int $extract_type = EXTR_OVERWRITE [, string $prefix ]] )

Import variables from an array into the current symbol table.

Checks each key to see whether it has a valid variable name. It also checks for collisions with existing variables in the symbol table.

example

/* 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";

compactCreate array containing variables and their values

array compact ( mixed $varname [, mixed $... ] )

Creates an array containing variables and their values.

For each of these, compact() looks for a variable with that name in the current symbol table and adds it to the output array such that the variable name becomes the key and the contents of the variable become the value for that key. In short, it does the opposite of extract().

Any strings that are not set will simply be skipped.

$city= "San Francisco";
$state = "CA";
$event = "SIGGRAPH";

$location_vars = array("city", "state");

$result = compact("event", "nothing_here", $location_vars);
print_r($result);


No comments:

Post a Comment