★ wanayoo — archive 1999 http://www.php.net/manual/function.compact.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to Arrays
Quick Reference
German version of this pageJapanese version of this pageItalian version of this pageFrench version of this pageHungarian version of this page
Arrays
* array
* array_count_values
* array_diff
* array_flip
* array_intersect
* array_keys
* array_merge
* array_merge_recursive
* array_multisort
* array_pad
* array_pop
* array_push
* array_rand
* array_reverse
* array_shift
* array_slice
* array_splice
* array_unique
* array_unshift
* array_values
* array_walk
* arsort
* asort
* compact
* count
* current
* each
* end
* extract
* in_array
* key
* krsort
* ksort
* list
* next
* pos
* prev
* range
* reset
* rsort
* shuffle
* sizeof
* sort
* uasort
* uksort
* usort
Manual: compact
View the source code for this pageSearch the site



Previous page
 asort
 Updated
Mon, 07 Aug 2000
count 
Next page


compact

(PHP4 )

compact --  Create array containing variables and their values

Description

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

Compact() takes a variable number of parameters. Each parameter can be either a string containing the name of the variable, or an array of variable names. The array can contain other arrays of variable names inside it; compact() handles it recursively.

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(). It returns the output array with all the variables added to it.

Example 1. Compact() example


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

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

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

After this, $result will be array ("event" => "SIGGRAPH", "city" => "San Francisco", "state" => "CA").

See also extract().


User Contributed Notes: compact


rlynch@ignitionstate.com
27-Jan-2000 12:43
It should be noted that PHP will simply skip any strings that are not set:
<PRE>
&lt;?php
$foo = 4;
$bar = 3;
$result = compact('foo', 'bar', 'baz');
?>
</PRE>
will result in:
('foo' => 4, 'bar' => 3)
'baz' is simply ignored.



 About Notes


Previous page
 asort
 Updated
Mon, 07 Aug 2000
count 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.