NOTE: the diff_array also removes all the duplicate values that match to the values in the second array:
<?
$array1 = array("a","b","c","a","a");
$array2 = array("a");
$diff = array_diff($array1,$array2);
// yields: array("b","c") the duplicate "a" values are removed
?>
array_diff_assoc
(PHP 4 >= 4.3.0, PHP 5)
array_diff_assoc — 追加された添字の確認を含めて配列の差を計算する
説明
array array_diff_assoc ( array array1, array array2 [, array ...] )array_diff_assoc() は、 array1 から他の引数の配列の中に現れない全ての値を含む array を返します。 array_diff() と異なり、 キーが比較に使用されることに注意してください。
例 217. array_diff_assoc() の例
<?php
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_diff_assoc($array1, $array2);
print_r($result);
?>
上の例の出力は以下となります。
Array
(
[b] => brown
[c] => blue
[0] => red
)
上の例で、"a" => "green" の組が両方の配列に現れており、 このため、この関数の出力には含まれていません。 これとは異なり、0 => "red" は出力の中にありますが、 これは、二番目の引数において "red" が 1 というキーを有しているためです。
key => value の組からの二つの値は、 (string) $elem1 === (string) $elem2 が成り立つ場合のみ等しいと見なされます。 言い替えると、厳密なチェックが行われるため、 文字列表現が同じである必要があります。
注意: この関数は、N 次元配列の一次元だけを調べることに注意してください。 もちろん、例えば、 array_diff_assoc($array1[0], $array2[0]); とすることにより、より深い次元でチェックを行うことも可能です。
array_diff()、 array_intersect() および array_intersect_assoc() も参照ください。
array_diff_assoc
31-May-2006 06:30
12-Jan-2005 02:56
Hi all,
For php versions < 4.3...
<?php
/**
* array_diff_assoc for version < 4.3
**/
if (!function_exists('array_diff_assoc'))
{
function array_diff_assoc($a1, $a2)
{
foreach($a1 as $key => $value)
{
if(isset($a2[$key]))
{
if((string) $value !== (string) $a2[$key])
{
$r[$key] = $value;
}
}else
{
$r[$key] = $value;
}
}
return $r ;
}
}
Hope there's no bug,
cheers
?>
09-Jun-2004 10:34
This functionality is now implemented in the PEAR package PHP_Compat.
More information about using this function without upgrading your version of PHP can be found on the below link:
http://pear.php.net/package/PHP_Compat
26-Oct-2003 04:20
[jochem at iamjochem dawt com]
Here is a slightly enhanced version of Micheal Johnsons function.
This version accepts arguments in the same way as
array_diff_assoc (i.e. you can pass as many arrays as you want - any
arguments that are not arrays are ignored). If the first argument is not an array you automatically get empty array back:
The point of the function is to return all values in the first array
whose keys (only keys are checked!) are not present in any subsequently passed arrays.
[original post]
array_diff_assoc() requires that both the key and the value pairs match. To match based on keys only, try this function.
<?php
function array_diff_keys()
{
$args = func_get_args();
$res = $args[0];
if(!is_array($res)) {
return array();
}
for($i=1;$i<count($args);$i++) {
if(!is_array($args[$i])) {
continue;
}
foreach ($args[$i] as $key => $data) {
unset($res[$key]);
}
}
return $res;
}
// Example
$a = array('a' => '1', 'b' => '2', 'c' => '3');
$b = array('a' => '2', 'b' => '2', 'e' => '4');
// Yields array('a' => '1', 'c' => '3')
// Note that the 'a' index is not removed (as one might expect)
$c = array_diff_assoc($a, $b);
// Yields array('c' => '3')
$d = array_diff_keys($a, $b);
?>
12-Jul-2003 04:10
[anders dot carlsson at mds dot mdh dot se]
The user contributed array_diff_assoc_recursive function is good except for the original array_diff_assoc always (?) returns an array.
Therefore I propose that $difference is initially set to an empty array (and the array is always returned), and the comparison against FALSE is replaced by count($new_diff). At least that's the modifications I made to run it they way my code expects.
[original post]
The following will recursively do an array_diff_assoc, which will calculate differences on a multi-dimensional level. (Forgive me if the braces do not line up, the note script did not like my tabs, and gave me trouble on some spaces.)
<?php
function array_diff_assoc_recursive($array1, $array2)
{
foreach($array1 as $key => $value)
{
if(is_array($value))
{
if(!is_array($array2[$key]))
{
$difference[$key] = $value;
}
else
{
$new_diff = array_diff_assoc_recursive($value, $array2[$key]);
if($new_diff != FALSE)
{
$difference[$key] = $new_diff;
}
}
}
elseif(!isset($array2[$key]) || $array2[$key] != $value)
{
$difference[$key] = $value;
}
}
return !isset($difference) ? 0 : $difference;
}
?>
09-May-2003 06:55
To unset elements in an array if you know the keys but not the values, you can do:
<?php
$a = array("foo", "bar", "baz", "quux");
$b = array(1, 3); // Elements to get rid of
foreach($b as $e)
unset($a[$e]);
?>
Of course this makes most sense if $b has many elements or is dynamically generated.