<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.array.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'es',
  ),
  'this' => 
  array (
    0 => 'function.compact.php',
    1 => 'compact',
    2 => 'Crea un array a partir de variables y su valor',
  ),
  'up' => 
  array (
    0 => 'ref.array.php',
    1 => 'Funciones de Arrays',
  ),
  'prev' => 
  array (
    0 => 'function.asort.php',
    1 => 'asort',
  ),
  'next' => 
  array (
    0 => 'function.count.php',
    1 => 'count',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'es',
    'path' => 'reference/array/functions/compact.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.compact" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">compact</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">compact</span> &mdash; <span class="dc-title">Crea un array a partir de variables y su valor</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.compact-description">
  <h3 class="title">Descripción</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>compact</strong></span>(<span class="methodparam"><span class="type"><span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.string.php" class="type string">string</a></span></span> <code class="parameter">$var_name</code></span>, <span class="methodparam"><span class="type"><span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.string.php" class="type string">string</a></span></span> <code class="parameter">...$var_names</code></span>): <span class="type"><a href="language.types.array.php" class="type array">array</a></span></div>

  <p class="para rdfs-comment">
   Crea un array a partir de variables y su valor.
  </p>
  <p class="para">
   Para cada uno de los argumentos <code class="parameter">varname</code>, <code class="parameter">...</code>,
   <span class="function"><strong>compact()</strong></span> busca una variable
   con el mismo nombre en la
   <a href="features.gc.refcounting-basics.php" class="link">tabla actual de símbolos</a>, y
   la añade al array, de manera que se tenga la relación nombre =&gt;
   &#039;valor de variable&#039;. En resumen, es lo contrario de la función
   <span class="function"><a href="function.extract.php" class="function">extract()</a></span>.
  </p>
  <blockquote class="note"><p><strong class="note">Nota</strong>: 
   <p class="para">
    Antes de PHP 7.3, todas las cadenas no definidas eran ignoradas en silencio.
   </p>
  </p></blockquote>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.compact-parameters">
  <h3 class="title">Parámetros</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">var_name</code></dt>
     <dt><code class="parameter">var_names</code></dt>
     <dd>
      <p class="para">
       <span class="function"><strong>compact()</strong></span> acepta diferentes parámetros <code class="parameter">varname</code>.
       Los parámetros pueden ser variables que contienen cadenas,
       o un array de cadenas, que puede contener otros arrays de nombres de
       variables, que <span class="function"><strong>compact()</strong></span> tratará de manera recursiva.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.compact-returnvalues">
  <h3 class="title">Valores devueltos</h3>
  <p class="para">
   Devuelve el array de salida que contiene todas las variables añadidas.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.compact-errors">
  <h3 class="title">Errores/Excepciones</h3>
  <p class="para">
   <span class="function"><strong>compact()</strong></span> emite un error de nivel <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> si una
   cadena dada hace referencia a una variable no definida.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.compact-changelog">
  <h3 class="title">Historial de cambios</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Versión</th>
      <th>Descripción</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       Si una cadena dada hace referencia a una variable no definida, se emite un error de nivel <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>.
      </td>
     </tr>

     <tr>
      <td>7.3.0</td>
      <td>
       <span class="function"><strong>compact()</strong></span> emite ahora un error de nivel <strong><code><a href="errorfunc.constants.php#constant.e-notice">E_NOTICE</a></code></strong>
       si una cadena dada hace referencia a una variable no definida. Anteriormente,
       estas cadenas eran ignoradas en silencio.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.compact-examples">
  <h3 class="title">Ejemplos</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Ejemplo #1 Ejemplo con <span class="function"><strong>compact()</strong></span></strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$city  </span><span style="color: #007700">= </span><span style="color: #DD0000">"San Francisco"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$state </span><span style="color: #007700">= </span><span style="color: #DD0000">"CA"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$event </span><span style="color: #007700">= </span><span style="color: #DD0000">"SIGGRAPH"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$location_vars </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"city"</span><span style="color: #007700">, </span><span style="color: #DD0000">"state"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">compact</span><span style="color: #007700">(</span><span style="color: #DD0000">"event"</span><span style="color: #007700">, </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 /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>El ejemplo anterior mostrará:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">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">Notas</h3>
  <blockquote class="note"><p><strong class="note">Nota</strong>: 
   <strong>Error común</strong><br />
   <p class="para">
    Debido a que las <a href="language.variables.variable.php" class="link">variables
    variables</a> no deben ser utilizadas con los
    <a href="language.variables.superglobals.php" class="link">arrays superglobales</a>
    en funciones, los arrays Superglobales no deben ser pasados
    a la función <span class="function"><strong>compact()</strong></span>.
   </p>
  </p></blockquote>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.compact-seealso">
  <h3 class="title">Ver también</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.extract.php" class="function" rel="rdfs-seeAlso">extract()</a> - Importa variables al s&iacute;mbolo actual desde un array</span></li>
   </ul>
  </p>
 </div>

</div><?php manual_footer($setup); ?>