mirror of
https://github.com/php/doc-ja.git
synced 2026-03-25 07:32:13 +01:00
199 lines
5.1 KiB
XML
199 lines
5.1 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision$ -->
|
|
<!-- EN-Revision: 2e60c5134e7a847c99f81eb3f7ecee1f5efeeace 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="changelog">
|
|
&reftitle.changelog;
|
|
<para>
|
|
<informaltable>
|
|
<tgroup cols="2">
|
|
<thead>
|
|
<row>
|
|
<entry>&Version;</entry>
|
|
<entry>&Description;</entry>
|
|
</row>
|
|
</thead>
|
|
<tbody>
|
|
&array.changelog.require-only-one;
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
</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>
|
|
<literal>(string) $elem1 === (string) $elem2</literal> の場合のみ、
|
|
つまり、<link linkend="language.types.string.casting">文字列表現</link> が同等な場合のみ、
|
|
2つの要素は等しいとみなされます。
|
|
</para>
|
|
|
|
<para>
|
|
<example>
|
|
<title>型が一致しない場合の <function>array_diff</function> の例</title>
|
|
<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') のインスタンスが一つ含まれます。
|
|
var_dump($result);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
別の比較関数を使いたい場合は、<function>array_udiff</function> を参照ください。
|
|
</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
|
|
-->
|