mirror of
https://github.com/php/doc-pt_br.git
synced 2026-03-24 07:02:09 +01:00
203 lines
5.3 KiB
XML
203 lines
5.3 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- EN-Revision: 2e60c5134e7a847c99f81eb3f7ecee1f5efeeace Maintainer: leonardolara Status: ready --><!-- CREDITS: felipe,lucasr,leonardolara -->
|
|
<refentry xml:id="function.each" xmlns="http://docbook.org/ns/docbook">
|
|
<refnamediv>
|
|
<refname>each</refname>
|
|
<refpurpose>Retorna o par atual de chave e valor de um array e avança o seu cursor</refpurpose>
|
|
</refnamediv>
|
|
|
|
<refsynopsisdiv>
|
|
&warn.deprecated.function-7-2-0.removed-8-0-0;
|
|
</refsynopsisdiv>
|
|
|
|
<refsect1 role="description">
|
|
&reftitle.description;
|
|
<methodsynopsis>
|
|
<type>array</type><methodname>each</methodname>
|
|
<methodparam><type class="union"><type>array</type><type>object</type></type><parameter role="reference">array</parameter></methodparam>
|
|
</methodsynopsis>
|
|
<para>
|
|
Retorna o par atual de chave e valor de um array e avança o seu
|
|
cursor.
|
|
</para>
|
|
<para>
|
|
Depois da execução de <function>each</function>, o cursor do
|
|
array irá apontar para o próximo elemento do array, ou após o último
|
|
elemento se ele atingir o final do array. Deve-se utilizar
|
|
<function>reset</function> se for desejado percorrer o array
|
|
novamente usando a função.
|
|
</para>
|
|
</refsect1>
|
|
<refsect1 role="parameters">
|
|
&reftitle.parameters;
|
|
<para>
|
|
<variablelist>
|
|
<varlistentry>
|
|
<term><parameter>array</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
O array de entrada.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
</variablelist>
|
|
</para>
|
|
</refsect1>
|
|
<refsect1 role="returnvalues">
|
|
&reftitle.returnvalues;
|
|
<para>
|
|
Retorna o par atual de chave e valor do
|
|
<parameter>array</parameter>. Este par é retornado em um
|
|
array de quatro elementos, com as chaves <literal>0</literal>, <literal>1</literal>,
|
|
<literal>key</literal> e <literal>value</literal>. Elementos
|
|
<literal>0</literal> e <literal>key</literal> contêm o nome da chave do
|
|
elemento do array; e <literal>1</literal> e <literal>value</literal> contêm
|
|
os dados.
|
|
</para>
|
|
<para>
|
|
Se o cursor interno do array estiver apontando para além do final do
|
|
conteúdo do array, <function>each</function> retorna
|
|
&false;.
|
|
</para>
|
|
</refsect1>
|
|
<refsect1 role="examples">
|
|
&reftitle.examples;
|
|
<para>
|
|
<example>
|
|
<title>Exemplos de <function>each</function></title>
|
|
<programlisting role="php" annotations="non-interactive">
|
|
<![CDATA[
|
|
<?php
|
|
$foo = array("bob", "fred", "jussi", "jouni", "egon", "marliese");
|
|
$bar = each($foo);
|
|
print_r($bar);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
<para>
|
|
<varname>$bar</varname> agora contém os seguintes pares de chaves e
|
|
valores:
|
|
</para>
|
|
<screen>
|
|
<![CDATA[
|
|
Array
|
|
(
|
|
[1] => bob
|
|
[value] => bob
|
|
[0] => 0
|
|
[key] => 0
|
|
)
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
<informalexample>
|
|
<programlisting role="php" annotations="non-interactive">
|
|
<![CDATA[
|
|
<?php
|
|
$foo = array("Robert" => "Bob", "Seppo" => "Sepi");
|
|
$bar = each($foo);
|
|
print_r($bar);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
<para>
|
|
<varname>$bar</varname> agora contém os seguintes pares de chaves e
|
|
valores:
|
|
</para>
|
|
<screen>
|
|
<![CDATA[
|
|
Array
|
|
(
|
|
[1] => Bob
|
|
[value] => Bob
|
|
[0] => Robert
|
|
[key] => Robert
|
|
)
|
|
]]>
|
|
</screen>
|
|
</informalexample>
|
|
</para>
|
|
<para>
|
|
<function>each</function> é tipicamente usada em conjunto com
|
|
<function>list</function> para percorrer um array, por
|
|
exemplo:
|
|
<example>
|
|
<title>Percorrendo um array com <function>each</function></title>
|
|
<programlisting role="php" annotations="non-interactive">
|
|
<![CDATA[
|
|
<?php
|
|
$fruta = array('a' => 'amora', 'b' => 'banana', 'c' => 'caju');
|
|
|
|
reset($fruta);
|
|
while (list($chave, $valor) = each($fruta)) {
|
|
echo "$chave => $valor\n";
|
|
}
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
a => amora
|
|
b => banana
|
|
c => caju
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
<caution>
|
|
<para>
|
|
Pelo fato da atribuição de um array a outra variável redefinir o ponteiro
|
|
original do array, o exemplo acima deveria causar um loop sem fim se a
|
|
variável <varname>$fruta</varname> fosse atribuída a outra variável dentro do
|
|
loop.
|
|
</para>
|
|
</caution>
|
|
<warning>
|
|
<para>
|
|
<function>each</function> também aceitará objetos, porém pode retornar resultados
|
|
inesperados. Portanto, não é recomendado iterar por propriedades de objeto
|
|
com <function>each</function>.
|
|
</para>
|
|
</warning>
|
|
</refsect1>
|
|
<refsect1 role="seealso">
|
|
&reftitle.seealso;
|
|
<para>
|
|
<simplelist>
|
|
<member><function>key</function></member>
|
|
<member><function>list</function></member>
|
|
<member><function>current</function></member>
|
|
<member><function>reset</function></member>
|
|
<member><function>next</function></member>
|
|
<member><function>prev</function></member>
|
|
<member>&foreach;</member>
|
|
<member><link linkend="language.oop5.iterations">Object Iteration</link></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
|
|
-->
|