mirror of
https://github.com/php/doc-pt_br.git
synced 2026-03-24 07:02:09 +01:00
286 lines
7.3 KiB
XML
286 lines
7.3 KiB
XML
<?xml version="1.0" encoding="UTF-8"?>
|
|
<!-- EN-Revision: eaec4ab10a65c4515ee2fb899d06e89bae3754b0 Maintainer: leonardolara Status: ready --><!-- CREDITS: lucasr,felipe,airtonzanon,leonardolara -->
|
|
<refentry xml:id="function.list" xmlns="http://docbook.org/ns/docbook">
|
|
<refnamediv>
|
|
<refname>list</refname>
|
|
<refpurpose>Cria variáveis como se fossem arrays</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>
|
|
Assim como <function>array</function>, não é exatamente uma função,
|
|
e sim uma construção da própria linguagem. <function>list</function> é usada
|
|
para criar uma lista de variáveis em apenas uma operação.
|
|
Somente arrays e objetos que implementam <link linkend="class.arrayaccess">ArrayAccess</link> podem ser desconstruídos.
|
|
Expressões <function>list</function> não podem ser completamente vazias.
|
|
</para>
|
|
<note>
|
|
<para>
|
|
Antes do PHP 7.1.0, <function>list</function> funcionava somente em arrays numéricos e assumia
|
|
que o índice numérico iniciava em 0.
|
|
</para>
|
|
</note>
|
|
<para>
|
|
A partir do PHP 7.1.0, <function>list</function> também pode conter chaves explícitas, permitindo a
|
|
desconstrução de arrays com chaves não inteiras ou não sequenciais. Para mais detalhes,
|
|
consulte a <link linkend="language.types.array.syntax.destructuring">seção sobre desconstrução de arrays</link>.
|
|
</para>
|
|
<note>
|
|
<para>
|
|
A tentativa de acessar uma chave de array que não foi definida é
|
|
o mesmo que acessar qualquer outra variável indefinida:
|
|
uma mensagem de erro de nível <constant>E_WARNING</constant>
|
|
(nível <constant>E_NOTICE</constant> antes do PHP 8.0.0) será
|
|
emitida e o resultado será &null;.
|
|
</para>
|
|
<para>
|
|
A tentativa de desconstruir um valor escalar atribui &null; a todas as variáveis.
|
|
A tentativa de desconstruir um objeto que não implementa ArrayAccess é um erro fatal.
|
|
</para>
|
|
</note>
|
|
</refsect1>
|
|
<refsect1 role="parameters">
|
|
&reftitle.parameters;
|
|
<para>
|
|
<variablelist>
|
|
<varlistentry>
|
|
<term><parameter>var</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
A variável.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
</variablelist>
|
|
<variablelist>
|
|
<varlistentry>
|
|
<term><parameter>vars</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
Outras variaveis.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
</variablelist>
|
|
</para>
|
|
</refsect1>
|
|
<refsect1 role="returnvalues">
|
|
&reftitle.returnvalues;
|
|
<para>
|
|
Retorna o array atribuído.
|
|
</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>
|
|
Foi adicionado suporte para atribuições de referência na deconstrução de array.
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>7.1.0</entry>
|
|
<entry>
|
|
Agora é possível especificar chaves em <function>list</function>. Isso
|
|
habilita desconstruir os arrays com chaves não-inteiras e não-sequenciais.
|
|
</entry>
|
|
</row>
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
</para>
|
|
</refsect1>
|
|
<refsect1 role="examples">
|
|
&reftitle.examples;
|
|
<para>
|
|
<example>
|
|
<title>Exemplo de <function>list</function></title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
$info = array('Café', 'marrom', 'cafeína');
|
|
|
|
// Listando todas as variáveis
|
|
list($bebida, $cor, $substancia) = $info;
|
|
echo "$bebida é $cor e $substancia o faz especial.\n";
|
|
|
|
// Listando apenas alguns deles
|
|
list($bebida, , $substancia) = $info;
|
|
echo "$bebida tem $substancia.\n";
|
|
|
|
// Ou ignoramos os primeiros valores para conseguir apenas o último
|
|
list( , , $substancia) = $info;
|
|
echo "I need $substancia!\n";
|
|
|
|
// list() não funciona com strings
|
|
list($bar) = "abcde";
|
|
var_dump($bar); // NULL
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>Exemplo de <function>list</function></title>
|
|
<programlisting role="php" annotations="non-interactive">
|
|
<![CDATA[
|
|
<?php
|
|
$resultado = $pdo->query("SELECT id, nome FROM empregados");
|
|
while (list($id, $nome) = $resultado->fetch(PDO::FETCH_NUM)) {
|
|
echo "id: $id, nome: $nome\n";
|
|
}
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title><function>list</function> e ordem das definições de índice </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>
|
|
A ordem em que são definidos os índices do array para
|
|
ser consumido por <function>list</function> é irrelevante.
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>Usando <function>list</function> com índices de array</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>
|
|
O retorno desse script é o seguinte (note a ordem dos elementos em
|
|
comparação com a ordem que foram escritos com a sintaxe <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> com chaves</title>
|
|
<simpara>
|
|
A partir do PHP 7.1.0, <function>list</function> também pode conter
|
|
chaves explícitas que podem ser informadas como expressões arbitrárias.
|
|
A mistura de chaves inteiras e strings é permitida; no entanto, elementos
|
|
com e sem chaves não podem ser misturados.
|
|
</simpara>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$dados = [
|
|
["id" => 1, "nome" => 'Mario'],
|
|
["id" => 2, "nome" => 'Frederico'],
|
|
];
|
|
foreach ($dados as ["id" => $id, "nome" => $nome]) {
|
|
echo "id: $id, nome: $nome\n";
|
|
}
|
|
echo PHP_EOL;
|
|
list(1 => $segundo, 3 => $quarto) = [1, 2, 3, 4];
|
|
echo "$segundo, $quarto\n";
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
id: 1, name: Mario
|
|
id: 2, name: Frederico
|
|
|
|
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
|
|
-->
|