mirror of
https://github.com/php/doc-es.git
synced 2026-03-24 15:32:36 +01:00
241 lines
6.7 KiB
XML
241 lines
6.7 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- EN-Revision: 4ca7cdeaee66bff2fefb9df88213ea6fc2e6f3dc Maintainer: PhilDaiguille Status: ready -->
|
|
<!-- Reviewed: no -->
|
|
<refentry xml:id="function.array-walk" xmlns="http://docbook.org/ns/docbook">
|
|
<refnamediv>
|
|
<refname>array_walk</refname>
|
|
<refpurpose>Ejecuta una función proporcionada por el usuario en cada uno de los elementos de un array</refpurpose>
|
|
</refnamediv>
|
|
<refsect1 role="description">
|
|
&reftitle.description;
|
|
<methodsynopsis>
|
|
<type>true</type><methodname>array_walk</methodname>
|
|
<methodparam><type class="union"><type>array</type><type>object</type></type><parameter role="reference">array</parameter></methodparam>
|
|
<methodparam><type>callable</type><parameter>callback</parameter></methodparam>
|
|
<methodparam choice="opt"><type>mixed</type><parameter>arg</parameter><initializer>&null;</initializer></methodparam>
|
|
</methodsynopsis>
|
|
<simpara>
|
|
Ejecuta la función <parameter>callback</parameter> definida por el usuario
|
|
en cada elemento del array <parameter>array</parameter>.
|
|
</simpara>
|
|
<para>
|
|
<function>array_walk</function> no es afectado por el puntero interno
|
|
del array <parameter>array</parameter>.
|
|
<function>array_walk</function> recorrerá el array en su totalidad
|
|
sin tener en cuenta la posición del puntero.
|
|
</para>
|
|
</refsect1>
|
|
<refsect1 role="parameters">
|
|
&reftitle.parameters;
|
|
<para>
|
|
<variablelist>
|
|
<varlistentry>
|
|
<term><parameter>array</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
El array de entrada.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>callback</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
Típicamente, <parameter>callback</parameter> toma dos parámetros.
|
|
El valor del parámetro <parameter>array</parameter>
|
|
siendo el primero y la clave/índice, el segundo.
|
|
</para>
|
|
<note>
|
|
<para>
|
|
Si <parameter>callback</parameter> debe trabajar con los verdaderos
|
|
valores del array, especifique que el primer parámetro de
|
|
<parameter>callback</parameter> debe ser pasado por
|
|
<link linkend="language.references">referencia</link>. Entonces, los
|
|
elementos serán directamente modificados en el array.
|
|
</para>
|
|
</note>
|
|
<note>
|
|
<para>
|
|
Varias funciones internas (por ejemplo, <function>strtolower</function>)
|
|
emiten una alerta si más argumentos que los esperados son pasados
|
|
a la función y no son utilizables directamente como
|
|
<parameter>callback</parameter>.
|
|
</para>
|
|
</note>
|
|
<para>
|
|
Solo los valores del <parameter>array</parameter> pueden ser modificados;
|
|
su estructura no puede ser modificada, es decir, no se pueden añadir, eliminar
|
|
o reordenar elementos. Si la función de callback no respeta esta
|
|
regla, el comportamiento se volverá indefinido e impredecible.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>arg</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
Si el parámetro opcional <parameter>arg</parameter>
|
|
es proporcionado, será pasado como tercer parámetro a la
|
|
función definida por el usuario <parameter>callback</parameter>.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
</variablelist>
|
|
</para>
|
|
</refsect1>
|
|
<refsect1 role="returnvalues">
|
|
&reftitle.returnvalues;
|
|
<para>
|
|
&return.true.always;
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="errors">
|
|
&reftitle.errors;
|
|
<para>
|
|
A partir de PHP 7.1.0, un <classname>ArgumentCountError</classname>
|
|
será lanzado si la función <parameter>callback</parameter> requiere más
|
|
de 2 parámetros (el valor y la clave del elemento del array),
|
|
o más de 3 si <parameter>arg</parameter> también es proporcionado.
|
|
Anteriormente, en este caso un error de nivel
|
|
<link linkend="errorfunc.constants">E_WARNING</link>
|
|
habría sido generado cada vez que la función
|
|
<function>array_walk</function> llama a <parameter>callback</parameter>.
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="changelog">
|
|
&reftitle.changelog;
|
|
<informaltable>
|
|
<tgroup cols="2">
|
|
<thead>
|
|
<row>
|
|
<entry>&Version;</entry>
|
|
<entry>&Description;</entry>
|
|
</row>
|
|
</thead>
|
|
<tbody>
|
|
&return.type.true;
|
|
<row>
|
|
<entry>8.0.0</entry>
|
|
<entry>
|
|
Si <parameter>callback</parameter> espera que el segundo o tercer
|
|
parámetro sea pasado por referencia, esta función ahora emite
|
|
una <constant>E_WARNING</constant>.
|
|
</entry>
|
|
</row>
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
</refsect1>
|
|
|
|
<refsect1 role="examples">
|
|
&reftitle.examples;
|
|
<para>
|
|
<example>
|
|
<title>Ejemplo con <function>array_walk</function></title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
|
|
|
|
function test_alter(&$item1, $key, $prefix)
|
|
{
|
|
$item1 = "$prefix: $item1";
|
|
}
|
|
|
|
function test_print($item2, $key)
|
|
{
|
|
echo "$key. $item2\n";
|
|
}
|
|
|
|
echo "Antes ...:\n";
|
|
array_walk($fruits, 'test_print');
|
|
|
|
array_walk($fruits, 'test_alter', 'fruit');
|
|
echo "... y después :\n";
|
|
|
|
array_walk($fruits, 'test_print');
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen role="php">
|
|
<![CDATA[
|
|
Antes ...:
|
|
d. lemon
|
|
a. orange
|
|
b. banana
|
|
c. apple
|
|
... y después :
|
|
d. fruit: lemon
|
|
a. fruit: orange
|
|
b. fruit: banana
|
|
c. fruit: apple
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>Ejemplo de <function>array_walk</function> con el uso de una función anónima</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$elements = ['a', 'b', 'c'];
|
|
|
|
array_walk($elements, function ($value, $key) {
|
|
echo "{$key} => {$value}\n";
|
|
});
|
|
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen role="php">
|
|
<![CDATA[
|
|
0 => a
|
|
1 => b
|
|
2 => c
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="seealso">
|
|
&reftitle.seealso;
|
|
<para>
|
|
<simplelist>
|
|
<member><function>array_walk_recursive</function></member>
|
|
<member><function>iterator_apply</function></member>
|
|
<member><function>list</function></member>
|
|
<member><function>each</function></member>
|
|
<member><function>call_user_func_array</function></member>
|
|
<member><function>array_map</function></member>
|
|
<member>&foreach;</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
|
|
-->
|