example:
$arr1 = array("color" => "red", "flavor" => "cherry");
$arr2 = array("color" => "cherry", "flavor" => "red");
print_r(array_diff($arr1, $arr2));

output:
Array()

those 2 arrays are different, but array_diff doesn't know it since it doesn't compare values at particular keys, just values in general. i want a function that knows that those 2 arrays are different; that the values for both color and flavor changed, and gives me back an array that looks exactly like $arr2. although i already wrote such a function, i'm wondering, does one already exist and i just can't find it in the manual? thanks. below find my solution, please comment if you are moved to do so.
/nick

function myArrayDiff($thing1, $thing2)
{
	$diff = array();
	while (list($key, $val) = each($thing1))
	{
		if ((string)$val !== (string)$thing2[$key])
			$diff[$key] = $val;
	}

return $diff;
}

    I haven't tested this at all. It's basically just a dual to yours.

    function AnotherArrayDiff($arr1,$arr2)
    {	$testarr=$arr1;
    	foreach($arr2 as $key=>$val)
    	{	if(isset($testarr[$key]) && $testarr[$key]==$val)
    			unset($test[$key]);
    	}
    	return $testarr;
    }
    

      thanks, its always nice to see 2 ways of doing something.
      (line 5, change "$test" to "$testarr" 🙂

        Write a Reply...