<?xml version="1.0" encoding="iso-8859-1"?> <!-- $Revision: 1.6 $ --> <!-- splitted from ./en/functions/array.xml, last change in rev 1.2 --> <refentry id="function.extract"> <refnamediv> <refname>extract</refname> <refpurpose> Import variables into the current symbol table from an array </refpurpose> </refnamediv> <refsect1> <title>Description</title> <methodsynopsis> <type>int</type><methodname>extract</methodname> <methodparam><type>array</type><parameter>var_array</parameter></methodparam> <methodparam choice="opt"><type>int</type><parameter>extract_type</parameter></methodparam> <methodparam choice="opt"><type>string</type><parameter>prefix</parameter></methodparam> </methodsynopsis> <para> This function is used to import variables from an array into the current symbol table. It takes an associative array <parameter>var_array</parameter> and treats keys as variable names and values as variable values. For each key/value pair it will create a variable in the current symbol table, subject to <parameter>extract_type</parameter> and <parameter>prefix</parameter> parameters. </para> <note> <para> Beginning with version 4.0.5, this function returns the number of variables extracted. </para> </note> <note> <para> EXTR_IF_EXISTS and EXTR_PREFIX_IF_EXISTS was introduced in version 4.2.0. </para> </note> <note> <para> EXTR_REFS was introduced in version 4.3.0. </para> </note> <para> <function>extract</function> checks each key to see whether it has a valid variable name. It also checks for collisions with existing variables in the symbol table. The way invalid/numeric keys and collisions are treated is determined by the <parameter>extract_type</parameter>. It can be one of the following values: <variablelist> <varlistentry> <term>EXTR_OVERWRITE</term> <listitem> <simpara> If there is a collision, overwrite the existing variable. </simpara> </listitem> </varlistentry> <varlistentry> <term>EXTR_SKIP</term> <listitem> <simpara> If there is a collision, don't overwrite the existing variable. </simpara> </listitem> </varlistentry> <varlistentry> <term>EXTR_PREFIX_SAME</term> <listitem> <simpara>If there is a collision, prefix the variable name with <parameter>prefix</parameter>. </simpara> </listitem> </varlistentry> <varlistentry> <term>EXTR_PREFIX_ALL</term> <listitem> <simpara> Prefix all variable names with <parameter>prefix</parameter>. Beginning with PHP 4.0.5, this includes numeric variables as well. </simpara> </listitem> </varlistentry> <varlistentry> <term>EXTR_PREFIX_INVALID</term> <listitem> <simpara> Only prefix invalid/numeric variable names with <parameter>prefix</parameter>. This flag was added in PHP 4.0.5. </simpara> </listitem> </varlistentry> <varlistentry> <term>EXTR_IF_EXISTS</term> <listitem> <simpara> Only overwrite the variable if it already exists in the current symbol table, otherwise do nothing. This is useful for defining a list of valid variables and then extracting only those variables you have defined out of $_REQUEST, for example. This flag was added in PHP 4.2.0. </simpara> </listitem> </varlistentry> <varlistentry> <term>EXTR_PREFIX_IF_EXISTS</term> <listitem> <simpara> Only create prefixed variable names if the non-prefixed version of the same variable exists in the current symbol table. This flag was added in PHP 4.2.0. </simpara> </listitem> </varlistentry> <varlistentry> <term>EXTR_REFS</term> <listitem> <simpara> Extracts variables as references. This effectively means that the values of the imported variables are still referencing the values of the <parameter>var_array</parameter> parameter. You can use this flag on its own or combine it with any other flag by OR'ing the <parameter>extract_type</parameter>. This flag was added in PHP 4.3.0. </simpara> </listitem> </varlistentry> </variablelist> </para> <para> If <parameter>extract_type</parameter> is not specified, it is assumed to be EXTR_OVERWRITE. </para> <para> Note that <parameter>prefix</parameter> is only required if <parameter>extract_type</parameter> is EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID or EXTR_PREFIX_IF_EXISTS. If the prefixed result is not a valid variable name, it is not imported into the symbol table. </para> <para> <function>extract</function> returns the number of variables successfully imported into the symbol table. </para> <para> A possible use for extract is to import into the symbol table variables contained in an associative array returned by <function>wddx_deserialize</function>. </para> <para> <example> <title><function>extract</function> example</title> <programlisting role="php"> <![CDATA[ <?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"); print "$color, $size, $shape, $wddx_size\n"; ?> ]]> </programlisting> </example> </para> <para> The above example will produce: <programlisting> <![CDATA[ blue, large, sphere, medium ]]> </programlisting> </para> <para> The <varname>$size</varname> wasn't overwritten, because we specified EXTR_PREFIX_SAME, which resulted in <varname>$wddx_size</varname> being created. If EXTR_SKIP was specified, then $wddx_size wouldn't even have been created. EXTR_OVERWRITE would have caused <varname>$size</varname> to have value "medium", and EXTR_PREFIX_ALL would result in new variables being named <varname>$wddx_color</varname>, <varname>$wddx_size</varname>, and <varname>$wddx_shape</varname>. </para> <para> You must use an associative array, a numerically indexed array will not produce results unless you use EXTR_PREFIX_ALL or EXTR_PREFIX_INVALID. </para> <para> See also <function>compact</function>. </para> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t indent-tabs-mode:nil sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 -->