Skip to content

Commit 27564db

Browse files
author
Yoshinari Takaoka
committed
Convert a note into a more robust example showing how type juggling works.
git-svn-id: https://svn.php.net/repository/phpdoc/ja/trunk@350491 c90b9560-bf6c-de11-be94-00142212c4b1
1 parent 58ee6f9 commit 27564db

File tree

1 file changed

+48
-9
lines changed

1 file changed

+48
-9
lines changed

reference/array/functions/array-diff.xml

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<!-- $Revision$ -->
3-
<!-- EN-Revision: 52ae7dd55e62b1e7b6920499ed7448892e09bb48 Maintainer: hirokawa Status: ready -->
4-
<!-- CREDITS: shimooka -->
3+
<!-- EN-Revision: b8990010918c977f62f147cf57a04c7e36f625e5 Maintainer: hirokawa Status: ready -->
4+
<!-- CREDITS: shimooka,mumumu -->
55
<refentry xml:id="function.array-diff" xmlns="http://docbook.org/ns/docbook">
66
<refnamediv>
77
<refname>array_diff</refname>
@@ -59,6 +59,7 @@
5959
<para>
6060
<parameter>array1</parameter>
6161
のエントリのうち、他のどの配列にも含まれない要素のみを含む配列を返します。
62+
<parameter>array1</parameter> の配列のキーは維持されます。
6263
</para>
6364
</refsect1>
6465

@@ -92,17 +93,54 @@ Array
9293
</screen>
9394
</example>
9495
</para>
96+
97+
<para>
98+
<example>
99+
<title>型が一致しない場合の <function>array_diff</function> の例</title>
100+
<para>
101+
<literal>(string) $elem1 === (string) $elem2</literal> の場合のみ、
102+
つまり、<link linkend="language.types.string.casting">文字列表現</link> が同等な場合のみ、
103+
2つの要素は等しいとみなされます。
104+
</para>
105+
<programlisting role="php">
106+
<![CDATA[
107+
<?php
108+
// 以下の例は、配列が文字列にキャストできないので警告が発生します
109+
$source = [1, 2, 3, 4];
110+
$filter = [3, 4, [5], 6];
111+
$result = array_diff($source, $filter);
112+
113+
// 一方で、以下の例は問題ありません。なぜなら、オブジェクトは文字列にキャストできるからです。
114+
class S {
115+
private $v;
116+
117+
public function __construct(string $v) {
118+
$this->v = $v;
119+
}
120+
121+
public function __toString() {
122+
return $this->v;
123+
}
124+
}
125+
126+
$source = [new S('a'), new S('b'), new S('c')];
127+
$filter = [new S('b'), new S('c'), new S('d')];
128+
129+
$result = array_diff($source, $filter);
130+
131+
// $result には、S('a') のインスタンスが一つ含まれます。
132+
?>
133+
]]>
134+
</programlisting>
135+
<para>
136+
別の比較関数を使いたい場合は、<function>array_udiff</function> を参照して下さい。
137+
</para>
138+
</example>
139+
</para>
95140
</refsect1>
96141

97142
<refsect1 role="notes">
98143
&reftitle.notes;
99-
<note>
100-
<para>
101-
二つの要素は、<literal>(string) $elem1 === (string) $elem2</literal>
102-
の場合のみ等しいと見直されます。言い換えると、文字列表現が同じ場合となります。
103-
<!-- TODO: example of it... -->
104-
</para>
105-
</note>
106144
<note>
107145
<para>
108146
この関数は n 次元配列の一つの次元しかチェックしません。
@@ -117,6 +155,7 @@ Array
117155
<para>
118156
<simplelist>
119157
<member><function>array_diff_assoc</function></member>
158+
<member><function>array_udiff</function></member>
120159
<member><function>array_intersect</function></member>
121160
<member><function>array_intersect_assoc</function></member>
122161
</simplelist>

0 commit comments

Comments
 (0)