Files
archived-doc-pt-br/reference/array/functions/array-udiff.xml
2024-10-15 13:35:10 -03:00

257 lines
7.4 KiB
XML

<?xml version="1.0" encoding="UTF-8"?>
<!-- EN-Revision: 56509d07ae636f076057f55bbb2572ab7b7a39eb Maintainer: lhsazevedo Status: ready--><!-- CREDITS: amandavale,lhsazevedo -->
<refentry xml:id="function.array-udiff" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>array_udiff</refname>
<refpurpose>Computa a diferença de arrays usando uma função de callback para comparação dos dados</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>array</type><methodname>array_udiff</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
<methodparam rep="repeat"><type>array</type><parameter>arrays</parameter></methodparam>
<methodparam><type>callable</type><parameter>value_compare_func</parameter></methodparam>
</methodsynopsis>
<para>
Computa a diferença de arrays usando uma função de callback para comparação
dos dados. Esta é contrária a <function>array_diff</function> que usa uma
função interna para comparar os dados.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>array</parameter></term>
<listitem>
<para>
O primeiro array.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>arrays</parameter></term>
<listitem>
<para>
Arrays para comparar.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>value_compare_func</parameter></term>
<listitem>
&sort.callback.description;
&sort.callback.description.presort;
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Retorna um array contendo todos os valores de <parameter>array</parameter>
que não estão presentes em qualquer dos outros argumentos.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Exemplo de <function>array_udiff</function> usando Objetos stdClass</title>
<programlisting role="php">
<![CDATA[
<?php
// Arrays para comprar
$array1 = array(new stdClass, new stdClass,
new stdClass, new stdClass,
);
$array2 = array(
new stdClass, new stdClass,
);
// Define algumas propriedades para cada objeto
$array1[0]->largura = 11; $array1[0]->altura = 3;
$array1[1]->largura = 7; $array1[1]->altura = 1;
$array1[2]->largura = 2; $array1[2]->altura = 9;
$array1[3]->largura = 5; $array1[3]->altura = 7;
$array2[0]->largura = 7; $array2[0]->altura = 5;
$array2[1]->largura = 9; $array2[1]->altura = 2;
function comparar_por_area($a, $b) {
$areaA = $a->largura * $a->altura;
$areaB = $b->largura * $b->altura;
if ($areaA < $areaB) {
return -1;
} elseif ($areaA > $areaB) {
return 1;
} else {
return 0;
}
}
print_r(array_udiff($array1, $array2, 'comparar_por_area'));
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Array
(
[0] => stdClass Object
(
[largura] => 11
[altura] => 3
)
[1] => stdClass Object
(
[largura] => 7
[altura] => 1
)
)
]]>
</screen>
</example>
</para>
<para>
<example>
<title>Exemplo de <function>array_udiff</function> usando Objetos DateTime</title>
<programlisting role="php">
<![CDATA[
<?php
class MeuCalendario {
public $livres = array();
public $reservadas = array();
public function __construct($semana = 'now') {
$inicio = new DateTime($semana);
$inicio->modify('Monday this week midnight');
$fim = clone $inicio;
$fim->modify('Friday this week midnight');
$intervalo = new DateInterval('P1D');
foreach (new DatePeriod($inicio, $intervalo, $fim) as $tempoLivre) {
$this->livres[] = $tempoLivre;
}
}
public function marcarCompromisso(DateTime $data, $nota) {
$this->reservadas[] = array('data' => $data->modify('midnight'), 'nota' => $nota);
}
public function checarDisponibilidade() {
return array_udiff($this->livres, $this->reservadas, array($this, 'compararPersonalizado'));
}
public function compararPersonalizado($livres, $reservadas) {
if (is_array($livres)) $a = $livres['data'];
else $a = $livres;
if (is_array($reservadas)) $b = $reservadas['data'];
else $b = $reservadas;
if ($a == $b) {
return 0;
} elseif ($a > $b) {
return 1;
} else {
return -1;
}
}
}
// Cria um calendário para compromissos semanais
$meuCalendario = new MeuCalendario;
// Marca alguns compromissos para esta semana
$meuCalendario->marcarCompromisso(new DateTime('Monday this week'), "Limpando apartamento do GoogleGuy.");
$meuCalendario->marcarCompromisso(new DateTime('Wednesday this week'), "Fazendo uma viagem de snowboarding.");
$meuCalendario->marcarCompromisso(new DateTime('Friday this week'), "Corrigindo código bugado.");
// Checar disponibilidade de dias comparando datas $reservadas com datas $livres
echo "Estou disponível nos seguintes dias desta semana...\n\n";
foreach ($meuCalendario->checarDisponibilidade() as $livre) {
echo $livre->format('l'), "\n";
}
echo "\n\n";
echo "Estou ocupado(a) nos seguintes dias desta semana...\n\n";
foreach ($meuCalendario->reservadas as $reservada) {
echo $reservada['data']->format('l'), ": ", $reservada['nota'], "\n";
}
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Estou disponível nos seguintes dias desta semana...
Tuesday
Thursday
Estou ocupado(a) nos seguintes dias desta semana...
Monday: Limpando apartamento do GoogleGuy.
Wednesday: Fazendo uma viagem de snowboarding.
Friday: Corrigindo código bugado.
]]>
</screen>
</example>
</para>
</refsect1>
<refsect1 role="notes">
&reftitle.notes;
<note>
<simpara>
Observe que esta função verifica somente uma dimensão de um array
n-dimensional. É claro que dimensões mais profundas podem ser verificadas
usando <literal>array_udiff($array1[0], $array2[0], "func_compara_dados");</literal>.
</simpara>
</note>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>array_diff</function></member>
<member><function>array_diff_assoc</function></member>
<member><function>array_diff_uassoc</function></member>
<member><function>array_udiff_assoc</function></member>
<member><function>array_udiff_uassoc</function></member>
<member><function>array_intersect</function></member>
<member><function>array_intersect_assoc</function></member>
<member><function>array_uintersect</function></member>
<member><function>array_uintersect_assoc</function></member>
<member><function>array_uintersect_uassoc</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
-->