I l@ve RuBoard Previous Section Next Section

compact

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

Creates an array containing variables from the current scope.

Returns:

Associative array

Description:

This function performs the opposite job of extract(). Given one or more variable names, it places each named variable into an associative array, with the key of each element being the name of a variable from the current scope and the value of each element being the value of that variable. The array thus constructed is then returned.

Each parameter can be either a string giving the name of a variable to place into the array, or an array of variable names. Arrays can be nested as deeply as you like; compact() recurses over them.

Variables are skipped if they are not set within the scope in which compact() is called.

Version:

PHP 4

See also:

extract() 

Example:

Place variables from the current scope into an array
$foo = 'This is foo'; 
$bar = 'This is bar'; 
function compact_tester() {
   global $foo; 

   $quux = 'This is quux'; 
   $new_array = compact('foo', 'bar', array('quux')); 
   return $new_array; 
} 

$array = compact_tester(); 
print_r($array); 

Output: 
Array 
(
    [foo] => This is foo 
    [quux] => This is quux 
) 
    I l@ve RuBoard Previous Section Next Section