mirror of
https://github.com/php/doc-es.git
synced 2026-03-23 23:12:09 +01:00
287 lines
7.2 KiB
XML
287 lines
7.2 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- EN-Revision: eaec4ab10a65c4515ee2fb899d06e89bae3754b0 Maintainer: PhilDaiguille Status: ready -->
|
|
<!-- Reviewed: no -->
|
|
<refentry xml:id="function.list" xmlns="http://docbook.org/ns/docbook">
|
|
<refnamediv>
|
|
<refname>list</refname>
|
|
<refpurpose>Asigna variables como si fueran un array</refpurpose>
|
|
</refnamediv>
|
|
<refsect1 role="description">
|
|
&reftitle.description;
|
|
<methodsynopsis>
|
|
<type>array</type><methodname>list</methodname>
|
|
<methodparam><type>mixed</type><parameter>var</parameter></methodparam>
|
|
<methodparam rep="repeat" choice="opt"><type>mixed</type><parameter>vars</parameter></methodparam>
|
|
</methodsynopsis>
|
|
<para>
|
|
Al igual que <function>array</function>, esto no es realmente una función,
|
|
sino una construcción del lenguaje. <function>list</function> se utiliza para
|
|
asignar una lista de variables en una sola operación.
|
|
Solo se pueden desempaquetar arrays y objetos que implementen <link linkend="class.arrayaccess">ArrayAccess</link>.
|
|
Las expresiones <function>list</function> no pueden estar completamente vacías.
|
|
</para>
|
|
<note>
|
|
<para>
|
|
Antes de PHP 7.1.0, <function>list</function> solo funcionaba con arrays numéricos y asumía
|
|
que los índices numéricos comenzaban en 0.
|
|
</para>
|
|
</note>
|
|
<para>
|
|
A partir de PHP 7.1.0, <function>list</function> también puede contener claves explícitas, permitiendo
|
|
la desestructuración de arrays con claves no enteras o no secuenciales. Para más detalles sobre
|
|
la desestructuración de arrays, consulte la <link linkend="language.types.array.syntax.destructuring">sección de desestructuración de arrays</link>.
|
|
</para>
|
|
<note>
|
|
<para>
|
|
Intentar acceder a una clave de array que no ha sido definida es
|
|
lo mismo que acceder a cualquier otra variable no definida:
|
|
se emitirá un mensaje de error de nivel <constant>E_WARNING</constant>
|
|
(nivel <constant>E_NOTICE</constant> antes de PHP 8.0.0) y
|
|
el resultado será &null;.
|
|
</para>
|
|
<para>
|
|
Intentar desempaquetar un escalar asigna &null; a todas las variables.
|
|
Intentar desempaquetar un objeto que no implementa ArrayAccess es un error fatal.
|
|
</para>
|
|
</note>
|
|
</refsect1>
|
|
<refsect1 role="parameters">
|
|
&reftitle.parameters;
|
|
<para>
|
|
<variablelist>
|
|
<varlistentry>
|
|
<term><parameter>var</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
Una variable.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
</variablelist>
|
|
<variablelist>
|
|
<varlistentry>
|
|
<term><parameter>vars</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
Variables adicionales.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
</variablelist>
|
|
</para>
|
|
</refsect1>
|
|
<refsect1 role="returnvalues">
|
|
&reftitle.returnvalues;
|
|
<para>
|
|
Devuelve el array asignado.
|
|
</para>
|
|
</refsect1>
|
|
<refsect1 role="changelog">
|
|
&reftitle.changelog;
|
|
<para>
|
|
<informaltable>
|
|
<tgroup cols="2">
|
|
<thead>
|
|
<row>
|
|
<entry>&Version;</entry>
|
|
<entry>&Description;</entry>
|
|
</row>
|
|
</thead>
|
|
<tbody>
|
|
<row>
|
|
<entry>7.3.0</entry>
|
|
<entry>
|
|
Se añadió soporte para asignaciones por referencia en la desestructuración de arrays.
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>7.1.0</entry>
|
|
<entry>
|
|
Ahora es posible especificar claves en <function>list</function>. Esto
|
|
permite la desestructuración de arrays con claves no enteras o no secuenciales.
|
|
</entry>
|
|
</row>
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
</para>
|
|
</refsect1>
|
|
<refsect1 role="examples">
|
|
&reftitle.examples;
|
|
<para>
|
|
<example>
|
|
<title>Ejemplos de <function>list</function></title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
$info = array('coffee', 'brown', 'caffeine');
|
|
|
|
// Listando todas las variables
|
|
list($drink, $color, $power) = $info;
|
|
echo "$drink is $color and $power makes it special.\n";
|
|
|
|
// Listando algunas de ellas
|
|
list($drink, , $power) = $info;
|
|
echo "$drink has $power.\n";
|
|
|
|
// O saltemos solo a la tercera
|
|
list( , , $power) = $info;
|
|
echo "I need $power!\n";
|
|
|
|
// list() no funciona con strings
|
|
list($bar) = "abcde";
|
|
var_dump($bar); // NULL
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>Un ejemplo de uso de <function>list</function></title>
|
|
<programlisting role="php" annotations="non-interactive">
|
|
<![CDATA[
|
|
<?php
|
|
$result = $pdo->query("SELECT id, name FROM employees");
|
|
while (list($id, $name) = $result->fetch(PDO::FETCH_NUM)) {
|
|
echo "id: $id, name: $name\n";
|
|
}
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>Uso de <function>list</function> anidado</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
list($a, list($b, $c)) = array(1, array(2, 3));
|
|
|
|
var_dump($a, $b, $c);
|
|
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
int(1)
|
|
int(2)
|
|
int(3)
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
El orden en el que se definen los índices del array a ser consumido por
|
|
<function>list</function> es irrelevante.
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title><function>list</function> y el orden de las definiciones de índices</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$foo = array(2 => 'a', 'foo' => 'b', 0 => 'c');
|
|
$foo[1] = 'd';
|
|
list($x, $y, $z) = $foo;
|
|
var_dump($foo, $x, $y, $z);
|
|
]]>
|
|
</programlisting>
|
|
<para>
|
|
Produce la siguiente salida (note el orden de los elementos comparado con
|
|
el orden en que fueron escritos en la sintaxis <function>list</function>):
|
|
</para>
|
|
<screen>
|
|
<![CDATA[
|
|
array(4) {
|
|
[2]=>
|
|
string(1) "a"
|
|
["foo"]=>
|
|
string(1) "b"
|
|
[0]=>
|
|
string(1) "c"
|
|
[1]=>
|
|
string(1) "d"
|
|
}
|
|
string(1) "c"
|
|
string(1) "d"
|
|
string(1) "a"
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title><function>list</function> con claves</title>
|
|
<simpara>
|
|
A partir de PHP 7.1.0 <function>list</function> ahora también puede contener
|
|
claves explícitas, que pueden ser dadas como expresiones arbitrarias.
|
|
Se permite mezclar claves enteras y string; sin embargo, no se pueden mezclar
|
|
elementos con y sin claves.
|
|
</simpara>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$data = [
|
|
["id" => 1, "name" => 'Tom'],
|
|
["id" => 2, "name" => 'Fred'],
|
|
];
|
|
foreach ($data as ["id" => $id, "name" => $name]) {
|
|
echo "id: $id, name: $name\n";
|
|
}
|
|
echo PHP_EOL;
|
|
list(1 => $second, 3 => $fourth) = [1, 2, 3, 4];
|
|
echo "$second, $fourth\n";
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
id: 1, name: Tom
|
|
id: 2, name: Fred
|
|
|
|
2, 4
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="seealso">
|
|
&reftitle.seealso;
|
|
<para>
|
|
<simplelist>
|
|
<member><function>each</function></member>
|
|
<member><function>array</function></member>
|
|
<member><function>extract</function></member>
|
|
</simplelist>
|
|
</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:"~/.phpdoc/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
|
|
-->
|