function.extract.html

来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 366 行

HTML
366
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Import variables into the current symbol table from an array</title>  <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.end.html">end</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.in-array.html">in_array</a></div> <div class="up"><a href="ref.array.html">Array Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="function.extract" class="refentry"> <div class="refnamediv">  <h1 class="refname">extract</h1>  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">extract</span> &mdash; <span class="dc-title">Import variables into the current symbol table from an array</span></p> </div> <div class="refsect1 description">  <h3 class="title">Description</h3>  <div class="methodsynopsis dc-description">   <span class="type">int</span> <span class="methodname"><b><b>extract</b></b></span>    ( <span class="methodparam"><span class="type">array</span> <tt class="parameter">$var_array</tt></span>   [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$extract_type</tt></span>   [, <span class="methodparam"><span class="type">string</span> <tt class="parameter">$prefix</tt></span>  ]] )</div>  <p class="para rdfs-comment">   Import variables from an array into the current symbol table.  </p>  <p class="para">   <b>extract()</b> checks each key to see whether it has a valid   variable name. It also checks for collisions with existing variables in   the symbol table.  </p> </div> <div class="refsect1 parameters">  <h3 class="title">Parameters</h3>  <p class="para">   <dl>    <dt>     <span class="term"><i><tt class="parameter">var_array</tt></i></span>     <dd>      <p class="para">       An associative array. This function 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       <i><tt class="parameter">extract_type</tt></i> and       <i><tt class="parameter">prefix</tt></i> parameters.      </p>      <p class="para">       You must use an associative array, a numerically indexed array       will not produce results unless you use <b><tt>EXTR_PREFIX_ALL</tt></b> or       <b><tt>EXTR_PREFIX_INVALID</tt></b>.      </p>     </dd>    </dt>    <dt>     <span class="term"><i><tt class="parameter">extract_type</tt></i></span>     <dd>      <p class="para">       The way invalid/numeric keys and collisions are treated is determined       by the <i><tt class="parameter">extract_type</tt></i>. It can be one of the       following values:       <dl>        <dt>         <span class="term"><b><tt>EXTR_OVERWRITE</tt></b></span>         <dd>          <span class="simpara">           If there is a collision, overwrite the existing variable.          </span>         </dd>        </dt>        <dt>         <span class="term"><b><tt>EXTR_SKIP</tt></b></span>         <dd>          <span class="simpara">           If there is a collision, don&#039;t overwrite the existing           variable.          </span>         </dd>        </dt>        <dt>         <span class="term"><b><tt>EXTR_PREFIX_SAME</tt></b></span>         <dd>          <span class="simpara">If there is a collision, prefix the variable name with          <i><tt class="parameter">prefix</tt></i>.          </span>         </dd>        </dt>        <dt>         <span class="term"><b><tt>EXTR_PREFIX_ALL</tt></b></span>         <dd>          <span class="simpara">           Prefix all variable names with           <i><tt class="parameter">prefix</tt></i>.          </span>         </dd>        </dt>        <dt>         <span class="term"><b><tt>EXTR_PREFIX_INVALID</tt></b></span>         <dd>          <span class="simpara">           Only prefix invalid/numeric variable names with           <i><tt class="parameter">prefix</tt></i>.          </span>         </dd>        </dt>        <dt>         <span class="term"><b><tt>EXTR_IF_EXISTS</tt></b></span>         <dd>          <span class="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           <var class="varname"><a href="reserved.variables.request.html" class="classname">$_REQUEST</a></var>, for example.          </span>         </dd>        </dt>        <dt>         <span class="term"><b><tt>EXTR_PREFIX_IF_EXISTS</tt></b></span>         <dd>          <span class="simpara">           Only create prefixed variable names if the non-prefixed version           of the same variable exists in the current symbol table.          </span>         </dd>        </dt>        <dt>         <span class="term"><b><tt>EXTR_REFS</tt></b></span>         <dd>          <span class="simpara">           Extracts variables as references. This effectively means that the           values of the imported variables are still referencing the values of           the <i><tt class="parameter">var_array</tt></i> parameter. You can use this flag           on its own or combine it with any other flag by OR&#039;ing the           <i><tt class="parameter">extract_type</tt></i>.          </span>         </dd>        </dt>       </dl>      </p>      <p class="para">       If <i><tt class="parameter">extract_type</tt></i> is not specified, it is       assumed to be <b><tt>EXTR_OVERWRITE</tt></b>.      </p>     </dd>    </dt>    <dt>     <span class="term"><i><tt class="parameter">prefix</tt></i></span>     <dd>      <p class="para">       Note that <i><tt class="parameter">prefix</tt></i> is only required if       <i><tt class="parameter">extract_type</tt></i> is <b><tt>EXTR_PREFIX_SAME</tt></b>,       <b><tt>EXTR_PREFIX_ALL</tt></b>, <b><tt>EXTR_PREFIX_INVALID</tt></b>       or <b><tt>EXTR_PREFIX_IF_EXISTS</tt></b>. If       the prefixed result is not a valid variable name, it is not       imported into the symbol table. Prefixes are automatically separated from       the array key by an underscore character.      </p>     </dd>    </dt>   </dl>  </p> </div> <div class="refsect1 returnvalues">  <h3 class="title">Return Values</h3>  <p class="para">   Returns the number of variables successfully imported into the symbol   table.  </p> </div> <div class="refsect1 changelog">  <h3 class="title">ChangeLog</h3>  <p class="para">   <table class="informaltable">    <colgroup>     <thead valign="middle">      <tr valign="middle">       <th colspan="1">Version</th>       <th colspan="1">Description</th>      </tr>     </thead>     <tbody valign="middle" class="tbody">      <tr valign="middle">       <td colspan="1" rowspan="1" align="left">4.3.0</td>       <td colspan="1" rowspan="1" align="left">        <b><tt>EXTR_REFS</tt></b> was added.       </td>      </tr>      <tr valign="middle">       <td colspan="1" rowspan="1" align="left">4.2.0</td>       <td colspan="1" rowspan="1" align="left">        <b><tt>EXTR_IF_EXISTS</tt></b> and <b><tt>EXTR_PREFIX_IF_EXISTS</tt></b>        were added.       </td>      </tr>      <tr valign="middle">       <td colspan="1" rowspan="1" align="left">4.0.5</td>       <td colspan="1" rowspan="1" align="left">        This function now returns the number of variables extracted.        <b><tt>EXTR_PREFIX_INVALID</tt></b> was added.        <b><tt>EXTR_PREFIX_ALL</tt></b> includes numeric variables as well.       </td>      </tr>     </tbody>    </colgroup>   </table>  </p> </div> <div class="refsect1 examples">  <h3 class="title">Examples</h3>  <p class="para">   <div class="example">    <p><b>Example #1 <b>extract()</b> example</b></p>    <div class="example-contents"><p>     A possible use for <b>extract()</b> is to import into the     symbol table variables contained in an associative array returned by     <a href="function.wddx-deserialize.html" class="function">wddx_deserialize()</a>.    </p></div>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">/*&nbsp;Suppose&nbsp;that&nbsp;$var_array&nbsp;is&nbsp;an&nbsp;array&nbsp;returned&nbsp;from<br />&nbsp;&nbsp;&nbsp;wddx_deserialize&nbsp;*/<br /><br /></span><span style="color: #0000BB">$size&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"large"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$var_array&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"color"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"blue"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"size"&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"medium"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"shape"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"sphere"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">extract</span><span style="color: #007700">(</span><span style="color: #0000BB">$var_array</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">EXTR_PREFIX_SAME</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"wddx"</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"$color,&nbsp;$size,&nbsp;$shape,&nbsp;$wddx_size\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>    <div class="example-contents"><p>The above example will output:</p></div>    <div class="example-contents"><pre><div class="cdata"><pre>blue, large, sphere, medium</pre></div>    </pre></div>    <div class="example-contents"><p>     The <var class="varname">$size</var> wasn&#039;t overwritten, because we specified     <b><tt>EXTR_PREFIX_SAME</tt></b>, which resulted in     <var class="varname">$wddx_size</var> being created.  If <b><tt>EXTR_SKIP</tt></b> was     specified, then $wddx_size wouldn&#039;t even have been created.     <b><tt>EXTR_OVERWRITE</tt></b> would have caused <var class="varname">$size</var> to have     value &quot;medium&quot;, and <b><tt>EXTR_PREFIX_ALL</tt></b> would result in new variables     being named <var class="varname">$wddx_color</var>,     <var class="varname">$wddx_size</var>, and     <var class="varname">$wddx_shape</var>.    </p></div>   </div>  </p> </div> <div class="refsect1 notes">  <h3 class="title">Notes</h3>  <div class="warning"><b class="warning">Warning</b>   <p class="para">    Do not use <b>extract()</b> on untrusted data, like user-input    (<var class="varname"><a href="reserved.variables.get.html" class="classname">$_GET</a></var>, ...). If you do, for example, if you want to run old code that    relies on <a href="security.globals.html" class="link">register_globals</a>    temporarily, make sure you use one of the non-overwriting    <i><tt class="parameter">extract_type</tt></i> values such as    <b><tt>EXTR_SKIP</tt></b> and be aware that you should extract    in the same order that&#039;s defined in    <a href="ini.core.html#ini.variables-order" class="link">variables_order</a> within the    <a href="ini.html" class="link"><var class="filename">php.ini</var></a>.   </p>  </div> </div> <div class="refsect1 seealso">  <h3 class="title">See Also</h3>  <p class="para">   <ul class="simplelist">    <li class="member"><a href="function.compact.html" class="function" rel="rdfs-seeAlso">compact()</a></li>   </ul>  </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.end.html">end</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.in-array.html">in_array</a></div> <div class="up"><a href="ref.array.html">Array Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?