1
0
mirror of https://github.com/php/doc-ru.git synced 2026-03-25 08:12:14 +01:00
Files
archived-doc-ru/reference/array/functions/array-diff.xml
2026-01-26 19:44:27 +03:00

201 lines
5.8 KiB
XML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?xml version="1.0" encoding="utf-8"?>
<!-- EN-Revision: 2e60c5134e7a847c99f81eb3f7ecee1f5efeeace Maintainer: shein Status: ready -->
<!-- Reviewed: no -->
<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>
Функция возвращает массив (<type>array</type>) с элементами
массива <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>
Функция считает элементы равными, только если равны
<link linkend="language.types.string.casting">строковые представления</link> значений:
<literal>(string) $elem1 === (string) $elem2</literal>.
</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);
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
-->