Sophie

Sophie

distrib > Mageia > 4 > i586 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 2309

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Create array containing variables and their values</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.asort.html">asort</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.count.html">count</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.compact" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">compact</h1>
  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">compact</span> &mdash; <span class="dc-title">Create array containing variables and their values</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.compact-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">array</span> <span class="methodname"><strong>compact</strong></span>
    ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$varname1</code></span>
   [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$...</code></span>
  ] )</div>

  <p class="para rdfs-comment">
   Creates an array containing variables and their values.
  </p>
  <p class="para">
   For each of these,  <span class="function"><strong>compact()</strong></span> 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  <span class="function"><a href="function.extract.html" class="function">extract()</a></span>.
  </p>
  <p class="para">
   Any strings that are not set will simply be skipped.
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.compact-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>

    <dt>

     <span class="term"><em><code class="parameter">varname1</code></em></span>
     <dd>

      <p class="para">
        <span class="function"><strong>compact()</strong></span> 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;  <span class="function"><strong>compact()</strong></span>
       handles it recursively.
      </p>
     </dd>

    </dt>

   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.compact-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns the output array with all the variables added to it.
  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.compact-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-5005">
    <p><strong>Example #1  <span class="function"><strong>compact()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$city&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"San&nbsp;Francisco"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$state&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"CA"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$event&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"SIGGRAPH"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$location_vars&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"city"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"state"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">compact</span><span style="color: #007700">(</span><span style="color: #DD0000">"event"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"nothing_here"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$location_vars</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<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 screen">
<div class="cdata"><pre>
Array
(
    [event] =&gt; SIGGRAPH
    [city] =&gt; San Francisco
    [state] =&gt; CA
)
</pre></div>
    </div>
   </div>
  </p>
 </div>

 <div class="refsect1 notes" id="refsect1-function.compact-notes">
  <h3 class="title">Notes</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <strong>Gotcha</strong><br />
   <p class="para">
    Because <a href="language.variables.variable.html" class="link">variable
    variables</a> may not be used with PHP&#039;s
    <a href="language.variables.superglobals.html" class="link">Superglobal
    arrays</a> within functions, the Superglobal arrays may not be passed
    into  <span class="function"><strong>compact()</strong></span>.
   </p>
  </p></blockquote>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.compact-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"> <span class="function"><a href="function.extract.html" class="function" rel="rdfs-seeAlso">extract()</a> - Import variables into the current symbol table from an array</span></li>
   </ul>
  </p>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.asort.html">asort</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.count.html">count</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>