mirror of
https://github.com/php/doc-ja.git
synced 2026-03-26 08:02:15 +01:00
git-svn-id: https://svn.php.net/repository/phpdoc/ja/trunk@134975 c90b9560-bf6c-de11-be94-00142212c4b1
145 lines
4.8 KiB
XML
145 lines
4.8 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision: 1.4 $ -->
|
|
<!-- sync: 1.2 -->
|
|
<refentry id="function.array-walk">
|
|
<refnamediv>
|
|
<refname>array_walk</refname>
|
|
<refpurpose>
|
|
配列の全ての要素にユーザー関数を適用する。
|
|
</refpurpose>
|
|
</refnamediv>
|
|
<refsect1>
|
|
<title>説明</title>
|
|
<methodsynopsis>
|
|
<type>bool</type><methodname>array_walk</methodname>
|
|
<methodparam><type>array</type><parameter>array</parameter></methodparam>
|
|
<methodparam><type>callback</type><parameter>function</parameter></methodparam>
|
|
<methodparam choice="opt"><type>mixed</type><parameter>userdata</parameter></methodparam>
|
|
</methodsynopsis>
|
|
<simpara>
|
|
&return.success;
|
|
</simpara>
|
|
<simpara>
|
|
<parameter>array</parameter>の各要素に<parameter>function</parameter>
|
|
という名前のユーザ定義関数を適用します。
|
|
<parameter>function</parameter>には、配列の値が最初の引数として
|
|
渡され、配列のキーが2番目のパラメータとして渡されます。
|
|
<parameter>userdata</parameter>が指定された場合、3番目の引数として
|
|
ユーザー関数に渡されます。
|
|
</simpara>
|
|
<simpara>
|
|
<parameter>function</parameter>が2つまたは3つを超える引数を必要とする場合、
|
|
<parameter>userdata</parameter>の指定によっては、
|
|
<function>array_walk</function>が<parameter>function</parameter>
|
|
をコールする度に<link linkend="errorfunc.constants">E_WARNING</link>レベルの
|
|
エラーが発行されます。これらのエラーは、
|
|
<function>array_walk</function>の前に
|
|
<link linkend="language.operators.errorcontrol">@</link>エラー演算子を付けるか
|
|
<function>error_reporting</function>を使用することにより
|
|
抑制されます。
|
|
</simpara>
|
|
<note>
|
|
<para>
|
|
<parameter>function</parameter>により配列の値そのものを変更する必要
|
|
がある場合、<parameter>function</parameter>の最初の引数は
|
|
<link linkend="language.references">参照</link>として
|
|
渡す必要があります。この場合、配列の要素に加えた変更は、配列自体
|
|
に対して行われます。
|
|
</para>
|
|
</note>
|
|
<note>
|
|
<para>
|
|
キー及びuserdataを<parameter>function</parameter>に渡す処理は、
|
|
バージョン4.0.0で追加されました。
|
|
</para>
|
|
</note>
|
|
<para>
|
|
<function>array_walk</function>は<parameter>array</parameter>の
|
|
内部配列ポインタに影響されません。<function>array_walk</function>は
|
|
ポインタの位置に関わらず配列の全てに渡って適用されます。
|
|
ポインタをリセットするには<function>reset</function>を
|
|
使用してください。PHP3では<function>array_walk</function>は
|
|
ポインタをリセットします。
|
|
</para>
|
|
<para>
|
|
コールバック関数により配列自身を変更することはできません。
|
|
例えば、要素の追加、削除、要素のunset等はできません。
|
|
<function>array_walk</function>が適用される配列を
|
|
変更しようとすると、関数の動作を定義できず、予期しない結果を
|
|
得ることになります。
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title><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<br>\n";
|
|
}
|
|
|
|
echo "Before ...:\n";
|
|
array_walk ($fruits, 'test_print');
|
|
|
|
array_walk ($fruits, 'test_alter', 'fruit');
|
|
echo "... and after:\n";
|
|
|
|
array_walk ($fruits, 'test_print');
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
<para>
|
|
上のプログラムは次のような出力となります:
|
|
</para>
|
|
<screen role="php">
|
|
<![CDATA[
|
|
Before ...:
|
|
d. lemon
|
|
a. orange
|
|
b. banana
|
|
c. apple
|
|
... and after:
|
|
d. fruit: lemon
|
|
a. fruit: orange
|
|
b. fruit: banana
|
|
c. fruit: apple
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
<simpara>
|
|
<function>list</function>,
|
|
<link linkend="control-structures.foreach">foreach</link>,
|
|
<function>each</function>,
|
|
<function>call_user_func_array</function>も参照してください。
|
|
</simpara>
|
|
</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:"../../../../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
|
|
-->
|