note 98680 modified in function.array-diff by danbrown
| From: | danbrown@php.net | Date: | Wed, 30 Jun 2010 12:32:40 +0000 |
| Subject: | note 98680 modified in function.array-diff by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-170595@lists.php.net to get a copy of this message | ||
Multidimensional array_diff
<?php
echo '<pre>';
$bdd['80395']=
array('80396','80397','80398','777');
$folder['80395']=
array('80396','80397','666','80398','154223');
$folder['80397']=
array('34','35','36','45','57');
echo '<hr>';
function multidimensional_array_diff($a1,$a2)
{
$r = array();
foreach ($a2 as $key => $second)
{
foreach ($a1 as $key => $first)
{
if (isset($a2[$key]))
{
foreach ($first as $first_value)
{
foreach ($second as $second_value)
{
if ($first_value == $second_value)
{
$true = true;
break;
}
}
if (!isset($true))
{
$r[$key][] = $first_value;
}
unset($true);
}
}
else
{
$r[$key] = $first;
}
}
}
return $r;
}
print_r(single_diff_assoc($folder,$bdd));
?>
RESULT :
Array
(
[80395] => Array
(
[0] => 666
[1] => 154223
)
[80397] => Array
(
[0] => 34
[1] => 35
[2] => 36
[3] => 45
[4] => 57
)
)
--was--
Multidimensional array_diff
echo '<pre>';
$bdd['80395']=
array('80396','80397','80398','777');
$folder['80395']=
array('80396','80397','666','80398','154223');
$folder['80397']=
array('34','35','36','45','57');
echo '<hr>';
function multidimensional_array_diff($a1,$a2)
{
$r = array();
foreach ($a2 as $key => $second)
{
foreach ($a1 as $key => $first)
{
if (isset($a2[$key]))
{
foreach ($first as $first_value)
{
foreach ($second as $second_value)
{
if ($first_value == $second_value)
{
$true = true;
break;
}
}
if (!isset($true))
{
$r[$key][] = $first_value;
}
unset($true);
}
}
else
{
$r[$key] = $first;
}
}
}
return $r;
}
print_r(single_diff_assoc($folder,$bdd));
RESULT :
Array
(
[80395] => Array
(
[0] => 666
[1] => 154223
)
[80397] => Array
(
[0] => 34
[1] => 35
[2] => 36
[3] => 45
[4] => 57
)
)
http://php.net/manual/en/function.array-diff.php