From: "Ashley M. Kirchner" on 14 Apr 2010 01:01 I have the following scenario: $array1 = array("12", "34", "56", "78", "90"); $array2 = array("12", "23", "56", "78", "89"); $result = array_diff($array1, $array2); print_r($result); This returns: Array ( [1] => 34 [4] => 90 ) However what I really want is a two-way comparison. I want elements that don't exist in either to be returned: 34 and 90 because they don't exist in $array2, AND 23 and 89 because they don't exist in $array1. So, is that a two step process of first doing an array_diff($array1, $array2) then reverse it by doing array_diff($array2, $array1) and merge/unique the results? Any caveats with that? $array1 = array("12", "34", "56", "78", "90"); $array2 = array("12", "23", "56", "78", "89"); $diff1 = array_diff($array1, $array2); $diff2 = array_diff($array2, $array1); $result = array_unique(array_merge($diff1, $diff2)); print_r($result); -- A
From: Rene Veerman on 14 Apr 2010 01:10 On Wed, Apr 14, 2010 at 7:01 AM, Ashley M. Kirchner <ashley(a)pcraft.com> wrote: > I have the following scenario: > > > > $array1 = array("12", "34", "56", "78", "90"); > > $array2 = array("12", "23", "56", "78", "89"); > > > > $result = array_diff($array1, $array2); > > > > print_r($result); > > > > > > This returns: > > > > Array > > ( > > [1] => 34 > > [4] => 90 > > ) > > > > > > However what I really want is a two-way comparison. I want elements that > don't exist in either to be returned: > > > > 34 and 90 because they don't exist in $array2, AND 23 and 89 because they > don't exist in $array1. So, is that a two step process of first doing an > array_diff($array1, $array2) then reverse it by doing array_diff($array2, > $array1) and merge/unique the results? Any caveats with that? > > > > $array1 = array("12", "34", "56", "78", "90"); > > $array2 = array("12", "23", "56", "78", "89"); > > > > $diff1 = array_diff($array1, $array2); > > $diff2 = array_diff($array2, $array1); > > > > $result = array_unique(array_merge($diff1, $diff2)); > > > > print_r($result); > > > > > > -- A > > ok, adding this to the todo-list for htmlMicroscope... ETA on delivery of 1.3.0-final: about 2 to 3 months i'm afraid. Gotta get a new laundromat for my home too and stuff like that :) -- --------------------------------- Greetings from Rene7705, I have made some free open source webcomponents designed and written by me available through: http://code.google.com/u/rene7705/ , or http://mediabeez.ws (latest dev versions, currently offline) Personal info about me is available through http://www.facebook.com/rene7705 ---------------------------------
From: Ashley Sheridan on 14 Apr 2010 04:39 On Tue, 2010-04-13 at 23:01 -0600, Ashley M. Kirchner wrote: > I have the following scenario: > > > > $array1 = array("12", "34", "56", "78", "90"); > > $array2 = array("12", "23", "56", "78", "89"); > > > > $result = array_diff($array1, $array2); > > > > print_r($result); > > > > > > This returns: > > > > Array > > ( > > [1] => 34 > > [4] => 90 > > ) > > > > > > However what I really want is a two-way comparison. I want elements that > don't exist in either to be returned: > > > > 34 and 90 because they don't exist in $array2, AND 23 and 89 because they > don't exist in $array1. So, is that a two step process of first doing an > array_diff($array1, $array2) then reverse it by doing array_diff($array2, > $array1) and merge/unique the results? Any caveats with that? > > > > $array1 = array("12", "34", "56", "78", "90"); > > $array2 = array("12", "23", "56", "78", "89"); > > > > $diff1 = array_diff($array1, $array2); > > $diff2 = array_diff($array2, $array1); > > > > $result = array_unique(array_merge($diff1, $diff2)); > > > > print_r($result); > > > > > > -- A > I don't see any problems with doing it that way. This will only work as you intended if both arrays have the same number of elements I believe, otherwise you might end up with a situation where your final array has duplicates of the same number: $array1 = $array(1, 2, 3, 4, 5, 6); $array2 = $aray(1, 3, 2, 5); Thanks, Ash http://www.ashleysheridan.co.uk
From: Nathan Rixham on 14 Apr 2010 06:03 Ashley Sheridan wrote: > On Tue, 2010-04-13 at 23:01 -0600, Ashley M. Kirchner wrote: >> >> However what I really want is a two-way comparison. I want elements that >> don't exist in either to be returned: >> > > > I don't see any problems with doing it that way. By some freak chance I made an array diff class about 2 weeks ago which covers what you need. attached :) usage: $diff = new ArrayDiff( $old , $new ); $diff->l; // deleted items $diff->r; // inserted items $diff->u; // unchanged items The script is optimised for huge arrays, thus it's slower for small arrays than the usual array_diff but with large arrays it's quicker. Regards Nathan
From: Rene Veerman on 14 Apr 2010 06:36 On Wed, Apr 14, 2010 at 12:03 PM, Nathan Rixham <nrixham(a)gmail.com> wrote: > Ashley Sheridan wrote: >> On Tue, 2010-04-13 at 23:01 -0600, Ashley M. Kirchner wrote: >>> >>> However what I really want is a two-way comparison. I want elements that >>> don't exist in either to be returned: >>> >> >> >> I don't see any problems with doing it that way. > > By some freak chance I made an array diff class about 2 weeks ago which > covers what you need. attached :) > > usage: > > $diff = new ArrayDiff( $old , $new ); > $diff->l; // deleted items > $diff->r; // inserted items > $diff->u; // unchanged items > > The script is optimised for huge arrays, thus it's slower for small > arrays than the usual array_diff but with large arrays it's quicker. > > Regards > > Nathan > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > nice one :) i'll put it in a work-preperation folder for htmlMicroscope then, one of these days :) -- --------------------------------- Greetings from Rene7705, I have made some free open source webcomponents designed and written by me available through: http://code.google.com/u/rene7705/ , or http://mediabeez.ws (latest dev versions, currently offline) Personal info about me is available through http://www.facebook.com/rene7705 ---------------------------------
|
Next
|
Last
Pages: 1 2 3 Prev: Basic switch statement Next: problems with feature '--with-pdo-oci' RPM (spec) |