<?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 => 'fr',
  ),
  'this' => 
  array (
    0 => 'function.list.php',
    1 => 'list',
    2 => 'Assigne des variables comme si elles &eacute;taient un tableau',
  ),
  'up' => 
  array (
    0 => 'ref.array.php',
    1 => 'Fonctions sur les tableaux',
  ),
  'prev' => 
  array (
    0 => 'function.ksort.php',
    1 => 'ksort',
  ),
  'next' => 
  array (
    0 => 'function.natcasesort.php',
    1 => 'natcasesort',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    'path' => 'reference/array/functions/list.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.list" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">list</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">list</span> &mdash; <span class="dc-title">Assigne des variables comme si elles étaient un tableau</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.list-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>list</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">$var</code></span>, <span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">...$vars</code><span class="initializer"> = ?</span></span>): <span class="type"><a href="language.types.array.php" class="type array">array</a></span></div>

  <p class="para rdfs-comment">
   Tout comme <span class="function"><a href="function.array.php" class="function">array()</a></span>, <span class="function"><strong>list()</strong></span> n&#039;est pas une
   véritable fonction, mais un élément de langage, qui permet de rassembler
   les variables <code class="parameter">varname</code>, <code class="parameter">...</code>
   sous forme de tableau, pour les assigner en une seule ligne.
   Seuls les tableaux et les objets qui implémentent <a href="class.arrayaccess.php" class="link">ArrayAccess</a> peuvent être déstructurés.
   Les expressions <span class="function"><strong>list()</strong></span> ne peuvent pas être complètement vides.
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Antérieur à PHP 7.1.0, <span class="function"><strong>list()</strong></span> fonctionne uniquement sur un tableau
    d&#039;indice numérique et suppose que l&#039;indice numérique commence à 0.
   </p>
  </p></blockquote>
  <p class="para">
   À partir de PHP 7.1.0, <span class="function"><strong>list()</strong></span> peut également contenir des clés explicites, permettant ainsi la
   déstructuration de tableaux avec des clés non entières ou non séquentielles. Pour plus de détails sur
   la déstructuration de tableaux, voir la <a href="language.types.array.php#language.types.array.syntax.destructuring" class="link">section sur la déstructuration des tableaux</a>.
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Tenter d’accéder à une clé de tableau qui n’a pas été définie est
    équivalent à accéder à toute autre variable non définie :
    un message d’erreur de niveau <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>
    (<strong><code><a href="errorfunc.constants.php#constant.e-notice">E_NOTICE</a></code></strong> avant PHP 8.0.0) sera émis,
    et le résultat sera <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>.
   </p>
   <p class="para">
    Tenter de déstructurer un scalaire affecte <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> à toutes les variables.
    Tenter de déstructurer un objet qui n’implémente pas ArrayAccess est une erreur fatale.
   </p>
  </p></blockquote>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.list-parameters">
  <h3 class="title">Liste de paramètres</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">var</code></dt>
     <dd>
      <p class="para">
       Une variable.
      </p>
     </dd>
    
   </dl>
   <dl>
    
     <dt><code class="parameter">vars</code></dt>
     <dd>
      <p class="para">
       Variables supplémentaires.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.list-returnvalues">
  <h3 class="title">Valeurs de retour</h3>
  <p class="para">
   Retourne le tableau assigné.
  </p>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.list-changelog">
  <h3 class="title">Historique</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Version</th>
       <th>Description</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>7.3.0</td>
       <td>
        Le support pour l&#039;assignement par référence dans la déstructuration
        de tableau a été ajouté.
       </td>
      </tr>

      <tr>
       <td>7.1.0</td>
       <td>
        Il est maintenant possible de spécifier les clés dans la 
        <span class="function"><strong>list()</strong></span>. Cela permet de déstructurer des tableaux 
        avec des clés non-entières ou non séquentielles.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.list-examples">
  <h3 class="title">Exemples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Exemple #1 Exemple avec <span class="function"><strong>list()</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 />$info </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'coffee'</span><span style="color: #007700">, </span><span style="color: #DD0000">'brown'</span><span style="color: #007700">, </span><span style="color: #DD0000">'caffeine'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Liste toutes les variables<br /></span><span style="color: #007700">list(</span><span style="color: #0000BB">$drink</span><span style="color: #007700">, </span><span style="color: #0000BB">$color</span><span style="color: #007700">, </span><span style="color: #0000BB">$power</span><span style="color: #007700">) = </span><span style="color: #0000BB">$info</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$drink</span><span style="color: #DD0000"> is </span><span style="color: #0000BB">$color</span><span style="color: #DD0000"> and </span><span style="color: #0000BB">$power</span><span style="color: #DD0000"> makes it special.\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Liste certaines variables<br /></span><span style="color: #007700">list(</span><span style="color: #0000BB">$drink</span><span style="color: #007700">, , </span><span style="color: #0000BB">$power</span><span style="color: #007700">) = </span><span style="color: #0000BB">$info</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$drink</span><span style="color: #DD0000"> has </span><span style="color: #0000BB">$power</span><span style="color: #DD0000">.\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Ou bien, n'utilisons que le troisième<br /></span><span style="color: #007700">list( , , </span><span style="color: #0000BB">$power</span><span style="color: #007700">) = </span><span style="color: #0000BB">$info</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"I need </span><span style="color: #0000BB">$power</span><span style="color: #DD0000">!\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// list() ne fonctionne pas avec les chaînes de caractères<br /></span><span style="color: #007700">list(</span><span style="color: #0000BB">$bar</span><span style="color: #007700">) = </span><span style="color: #DD0000">"abcde"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$bar</span><span style="color: #007700">); </span><span style="color: #FF8000">// NULL<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2">
    <p><strong>Exemple #2 Exemple d&#039;utilisation de <span class="function"><strong>list()</strong></span></strong></p>
    <div class="example-contents">
<div class="annotation-non-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$result </span><span style="color: #007700">= </span><span style="color: #0000BB">$pdo</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT id, name FROM employees"</span><span style="color: #007700">);<br />while (list(</span><span style="color: #0000BB">$id</span><span style="color: #007700">, </span><span style="color: #0000BB">$name</span><span style="color: #007700">) = </span><span style="color: #0000BB">$result</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch</span><span style="color: #007700">(</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">FETCH_NUM</span><span style="color: #007700">)) {<br />    echo </span><span style="color: #DD0000">"id: </span><span style="color: #0000BB">$id</span><span style="color: #DD0000">, name: </span><span style="color: #0000BB">$name</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-3">
    <p><strong>Exemple #3 Utilisation d&#039;un sous-<span class="function"><strong>list()</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 /></span><span style="color: #007700">list(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, list(</span><span style="color: #0000BB">$b</span><span style="color: #007700">, </span><span style="color: #0000BB">$c</span><span style="color: #007700">)) = array(</span><span style="color: #0000BB">1</span><span style="color: #007700">, array(</span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">$b</span><span style="color: #007700">, </span><span style="color: #0000BB">$c</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>L&#039;exemple ci-dessus va afficher :</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">int(1)
int(2)
int(3)</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
    L&#039;ordre dans lequel les indices d&#039;un tableau à traiter par
    <span class="function"><strong>list()</strong></span> sont définis est non pertinent.
  </p>
  <p class="para">
   <div class="example" id="example-4">
    <p><strong>Exemple #4 <span class="function"><strong>list()</strong></span> et l&#039;ordre de définition des index</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$foo </span><span style="color: #007700">= array(</span><span style="color: #0000BB">2 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'a'</span><span style="color: #007700">, </span><span style="color: #DD0000">'foo' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'b'</span><span style="color: #007700">, </span><span style="color: #0000BB">0 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'c'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$foo</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">] = </span><span style="color: #DD0000">'d'</span><span style="color: #007700">;<br />list(</span><span style="color: #0000BB">$x</span><span style="color: #007700">, </span><span style="color: #0000BB">$y</span><span style="color: #007700">, </span><span style="color: #0000BB">$z</span><span style="color: #007700">) = </span><span style="color: #0000BB">$foo</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$foo</span><span style="color: #007700">, </span><span style="color: #0000BB">$x</span><span style="color: #007700">, </span><span style="color: #0000BB">$y</span><span style="color: #007700">, </span><span style="color: #0000BB">$z</span><span style="color: #007700">);</span></span></code></div>
    </div>

    <div class="example-contents"><p>
     Étant donné la sortie suivante (notez l&#039;ordre des éléments comparés
     et dans quel ordre ils ont été écrits dans la syntaxe 
     de la <span class="function"><strong>list()</strong></span>) :
    </p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">array(4) {
  [2]=&gt;
  string(1) &quot;a&quot;
  [&quot;foo&quot;]=&gt;
  string(1) &quot;b&quot;
  [0]=&gt;
  string(1) &quot;c&quot;
  [1]=&gt;
  string(1) &quot;d&quot;
}
string(1) &quot;c&quot;
string(1) &quot;d&quot;
string(1) &quot;a&quot;</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-5">
    <p><strong>Exemple #5 <span class="function"><strong>list()</strong></span> avec des clés</strong></p>
    <div class="example-contents"><p>
     À partir de PHP 7.1.0 <span class="function"><strong>list()</strong></span> peut maintenant contenir également
     des clés explicites, qui peuvent être donnés comme des expressions arbitraires. 
     Le mixage des clés <a href="language.types.integer.php" class="link">entier</a> et <a href="language.types.string.php" class="link">chaîne de caractères</a> est autorisé; Toutefois, les éléments 
     avec et sans clés ne peuvent pas être mélangés.
    </p></div>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$data </span><span style="color: #007700">= [<br />    [</span><span style="color: #DD0000">"id" </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #DD0000">"name" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Tom'</span><span style="color: #007700">],<br />    [</span><span style="color: #DD0000">"id" </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #DD0000">"name" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Fred'</span><span style="color: #007700">],<br />];<br />foreach (</span><span style="color: #0000BB">$data </span><span style="color: #007700">as [</span><span style="color: #DD0000">"id" </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$id</span><span style="color: #007700">, </span><span style="color: #DD0000">"name" </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$name</span><span style="color: #007700">]) {<br />    echo </span><span style="color: #DD0000">"id: </span><span style="color: #0000BB">$id</span><span style="color: #DD0000">, name: </span><span style="color: #0000BB">$name</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />}<br />echo </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />list(</span><span style="color: #0000BB">1 </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$second</span><span style="color: #007700">, </span><span style="color: #0000BB">3 </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$fourth</span><span style="color: #007700">) = [</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #0000BB">4</span><span style="color: #007700">];<br />echo </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$second</span><span style="color: #DD0000">, </span><span style="color: #0000BB">$fourth</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;</span></span></code></div>
    </div>

    <div class="example-contents"><p>L&#039;exemple ci-dessus va afficher :</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">id: 1, name: Tom
id: 2, name: Fred

2, 4</pre>
</div>
    </div>
   </div>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.list-seealso">
  <h3 class="title">Voir aussi</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.each.php" class="function" rel="rdfs-seeAlso">each()</a> - Retourne chaque paire cl&eacute;/valeur d'un tableau</span></li>
    <li><span class="function"><a href="function.array.php" class="function" rel="rdfs-seeAlso">array()</a> - Cr&eacute;e un tableau</span></li>
    <li><span class="function"><a href="function.extract.php" class="function" rel="rdfs-seeAlso">extract()</a> - Importe les variables dans la table des symboles</span></li>
   </ul>
  </p>
 </div>

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