1
0
mirror of https://github.com/php/doc-ja.git synced 2026-03-25 07:32:13 +01:00
Files
archived-doc-ja/reference/array/functions/array-diff.xml
Yoshinari Takaoka c58bf47244 Proper variadics instead of pseudo variadics
We markup variadic parameters with the `rep=repeat` standard DocBook
attribute of `<methodparam>`, and use proper variable names instead of
using the old pseudo variable name `...`.


git-svn-id: https://svn.php.net/repository/phpdoc/ja/trunk@351140 c90b9560-bf6c-de11-be94-00142212c4b1
2020-11-02 16:41:17 +00:00

178 lines
4.7 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 9e0f03ac354d797d1d16c0fcc1663e5e170f2727 Maintainer: hirokawa Status: ready -->
<!-- CREDITS: shimooka,mumumu -->
<refentry xml:id="function.array-diff" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>array_diff</refname>
<refpurpose>配列の差を計算する</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>array</type><methodname>array_diff</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
<methodparam rep="repeat"><type>array</type><parameter>arrays</parameter></methodparam>
</methodsynopsis>
<para>
<parameter>array</parameter> を他の配列と比較し、
<parameter>array</parameter> の要素の中で他の配列には存在しないものだけを返します。
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>array</parameter></term>
<listitem>
<para>
比較元の配列。
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>arrays</parameter></term>
<listitem>
<para>
比較対象の配列。
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
<parameter>array</parameter>
のエントリのうち、他のどの配列にも含まれない要素のみを含む配列を返します。
<parameter>array</parameter> の配列のキーは維持されます。
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>array_diff</function> の例</title>
<programlisting role="php">
<![CDATA[
<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
print_r($result);
?>
]]>
</programlisting>
<para>
<varname>$array1</varname> に複数存在する場合でも全て同様に処理されます。
この出力は次の通りです。
</para>
<screen>
<![CDATA[
Array
(
[1] => blue
)
]]>
</screen>
</example>
</para>
<para>
<example>
<title>型が一致しない場合の <function>array_diff</function> の例</title>
<para>
<literal>(string) $elem1 === (string) $elem2</literal> の場合のみ、
つまり、<link linkend="language.types.string.casting">文字列表現</link> が同等な場合のみ、
2つの要素は等しいとみなされます。
</para>
<programlisting role="php">
<![CDATA[
<?php
// 以下の例は、配列が文字列にキャストできないので警告が発生します
$source = [1, 2, 3, 4];
$filter = [3, 4, [5], 6];
$result = array_diff($source, $filter);
// 一方で、以下の例は問題ありません。なぜなら、オブジェクトは文字列にキャストできるからです。
class S {
private $v;
public function __construct(string $v) {
$this->v = $v;
}
public function __toString() {
return $this->v;
}
}
$source = [new S('a'), new S('b'), new S('c')];
$filter = [new S('b'), new S('c'), new S('d')];
$result = array_diff($source, $filter);
// $result には、S('a') のインスタンスが一つ含まれます。
?>
]]>
</programlisting>
<para>
別の比較関数を使いたい場合は、<function>array_udiff</function> を参照して下さい。
</para>
</example>
</para>
</refsect1>
<refsect1 role="notes">
&reftitle.notes;
<note>
<para>
この関数は n 次元配列の一つの次元しかチェックしません。
もちろん、<literal>array_diff($array1[0], $array2[0]);</literal>
のようにすることでより深い次元でのチェックもできます。
</para>
</note>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>array_diff_assoc</function></member>
<member><function>array_udiff</function></member>
<member><function>array_intersect</function></member>
<member><function>array_intersect_assoc</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
-->