Re: Re: wanted: array_diff for more than values

From: Date: Sat, 09 Nov 2002 00:26:24 +0000
Subject: Re: Re: wanted: array_diff for more than values
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-123521@lists.php.net to get a copy of this message
"Kevin Stone" <kevin@helpelf.com> wrote: > I think you're trying to defy logic here. How will the function know that > you want to keep color=>cherry or flavor=>cherry? What logical construct > makes that decision? The example you've given will result in an array that > looks eactly like $arr1 regardless of the content in either array. i beg to diff-er (haha, i'm so punny:). the function i wrote works the way i want it to, but perhaps i neglected to mention that it only works on purely associative arrays, i.e. string keys only. try this one: $arr1 = array("name" => "Bing", "color" => "red", "flavor" => "cherry"); $arr2 = array("name" => "Bing", "color" => "cherry", "flavor" => "red"); print_r(myArrayDiff($arr2, $arr1)); output: Array ( [color] => cherry [flavor] => red ) in words, the difference between arr2 and arr1 is that "color" changed to "cherry", and "flavor" changed to "red". /nick > > function myArrayDiff($thing1, $thing2) > > { > > $diff = array(); > > while (list($key, $val) = each($thing1)) > > { > > if ((string)$val !== (string)$thing2[$key]) > > $diff[$key] = $val; > > } > > > > return $diff; > > } > >

« previous php.general (#123521) next »