Array differences

Array differences

am 14.04.2010 07:01:38 von Ashley

------=_NextPart_000_0001_01CADB5D.4C8D71A0
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit

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


------=_NextPart_000_0001_01CADB5D.4C8D71A0--

Re: Array differences

am 14.04.2010 07:10:33 von Rene Veerman

On Wed, Apr 14, 2010 at 7:01 AM, Ashley M. Kirchner wro=
te:
> I have the following scenario:
>
>
>
> =A0 =A0 $array1 =3D array("12", "34", "56", "78", "90");
>
> =A0 =A0 $array2 =3D array("12", "23", "56", "78", "89");
>
>
>
> =A0 =A0 $result =3D array_diff($array1, $array2);
>
>
>
> =A0 =A0 print_r($result);
>
>
>
>
>
> This returns:
>
>
>
> =A0 =A0 Array
>
> =A0 =A0 (
>
> =A0 =A0 =A0 =A0 [1] =3D> 34
>
> =A0 =A0 =A0 =A0 [4] =3D> 90
>
> =A0 =A0 )
>
>
>
>
>
> However what I really want is a two-way comparison. =A0I want elements th=
at
> 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. =A0So, 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? =A0Any caveats with that?
>
>
>
> =A0 =A0 $array1 =3D array("12", "34", "56", "78", "90");
>
> =A0 =A0 $array2 =3D array("12", "23", "56", "78", "89");
>
>
>
> =A0 =A0 $diff1 =3D array_diff($array1, $array2);
>
> =A0 =A0 $diff2 =3D array_diff($array2, $array1);
>
>
>
> =A0 =A0 $result =3D array_unique(array_merge($diff1, $diff2));
>
>
>
> =A0 =A0 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 :)

--=20
---------------------------------
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/rene770=
5
---------------------------------

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Array differences

am 14.04.2010 10:39:35 von Ashley Sheridan

--=-EnRi+1xcn9vWHVCMFYp9
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

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



--=-EnRi+1xcn9vWHVCMFYp9--

Re: Array differences

am 14.04.2010 12:03:47 von Nathan Rixham

--------------050403000209010505060908
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

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


--------------050403000209010505060908
Content-Type: text/plain; charset=us-ascii

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--------------050403000209010505060908--

Re: Array differences

am 14.04.2010 12:36:45 von Rene Veerman

On Wed, Apr 14, 2010 at 12:03 PM, Nathan Rixham 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. =A0I 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 =3D 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 :)

--=20
---------------------------------
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/rene770=
5
---------------------------------

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Array differences

am 14.04.2010 16:29:42 von Ashley

On 4/14/2010 2:39 AM, Ashley Sheridan wrote:
> On Tue, 2010-04-13 at 23:01 -0600, Ashley M. Kirchner wrote:
>> $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);
>>
>
> 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);

Wouldn't array_unique() take care of that though? Your example
above returns 4 and 6, which would be correct.

A

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Array differences

am 14.04.2010 16:45:02 von Ryan Sun

Maybe this one works?
array_diff(array_unique($array1 + $array2), array_intersect($array1, $array=
2))

On Wed, Apr 14, 2010 at 4:39 AM, Ashley Sheridan
wrote:
> On Tue, 2010-04-13 at 23:01 -0600, Ashley M. Kirchner wrote:
>
>> I have the following scenario:
>>
>>
>>
>> =A0 =A0 =A0$array1 =3D array("12", "34", "56", "78", "90");
>>
>> =A0 =A0 =A0$array2 =3D array("12", "23", "56", "78", "89");
>>
>>
>>
>> =A0 =A0 =A0$result =3D array_diff($array1, $array2);
>>
>>
>>
>> =A0 =A0 =A0print_r($result);
>>
>>
>>
>>
>>
>> This returns:
>>
>>
>>
>> =A0 =A0 =A0Array
>>
>> =A0 =A0 =A0(
>>
>> =A0 =A0 =A0 =A0 =A0[1] =3D> 34
>>
>> =A0 =A0 =A0 =A0 =A0[4] =3D> 90
>>
>> =A0 =A0 =A0)
>>
>>
>>
>>
>>
>> However what I really want is a two-way comparison. =A0I want elements t=
hat
>> don't exist in either to be returned:
>>
>>
>>
>> 34 and 90 because they don't exist in $array2, AND 23 and 89 because the=
y
>> don't exist in $array1. =A0So, 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? =A0Any caveats with that?
>>
>>
>>
>> =A0 =A0 =A0$array1 =3D array("12", "34", "56", "78", "90");
>>
>> =A0 =A0 =A0$array2 =3D array("12", "23", "56", "78", "89");
>>
>>
>>
>> =A0 =A0 =A0$diff1 =3D array_diff($array1, $array2);
>>
>> =A0 =A0 =A0$diff2 =3D array_diff($array2, $array1);
>>
>>
>>
>> =A0 =A0 =A0$result =3D array_unique(array_merge($diff1, $diff2));
>>
>>
>>
>> =A0 =A0 =A0print_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 =3D $array(1, 2, 3, 4, 5, 6);
> $array2 =3D $aray(1, 3, 2, 5);
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>
>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: Array differences

am 14.04.2010 17:37:45 von Ashley

No because that only does a one-way comparison. It only tells me what's
missing from $array2. I need it from both arrays. That's why I'm =
comparing
1 versus 2, then 2 versus 1, and then doing a merge/unique on the =
result.

$array1 =3D array(1, 2, 3, 4, 5, 6);
$array2 =3D array(1, 3, 2, 8, 9);
$result =3D array_diff(array_unique($array1 + $array2),
array_intersect($array1, $array2));

=3D> (4, 5, 6)


Versus:

$array1 =3D array(1, 2, 3, 4, 5, 6);
$array2 =3D array(1, 3, 2, 8, 9);
$diff1 =3D array_diff($array1, $array2);
$diff2 =3D array_diff($array2, $array1);
$result =3D array_unique(array_merge($diff1, $diff2));

=3D> (4, 5, 6, 8, 9)

This second $result is what I want. So far I haven't noticed any =
problems
doing it this way ... yet. I'm sure someone will tell me otherwise.

Ash

> -----Original Message-----
> From: Ryan Sun [mailto:ryansun81@gmail.com]
> Sent: Wednesday, April 14, 2010 8:45 AM
> To: ash@ashleysheridan.co.uk
> Cc: Ashley M. Kirchner; php-general@lists.php.net
> Subject: Re: [PHP] Array differences
>=20
> Maybe this one works?
> array_diff(array_unique($array1 + $array2), array_intersect($array1,
> $array2))
>=20
> On Wed, Apr 14, 2010 at 4:39 AM, Ashley Sheridan
> wrote:
> > On Tue, 2010-04-13 at 23:01 -0600, Ashley M. Kirchner wrote:
> >
> >> I have the following scenario:
> >>
> >>
> >>
> >> =A0 =A0 =A0$array1 =3D array("12", "34", "56", "78", "90");
> >>
> >> =A0 =A0 =A0$array2 =3D array("12", "23", "56", "78", "89");
> >>
> >>
> >>
> >> =A0 =A0 =A0$result =3D array_diff($array1, $array2);
> >>
> >>
> >>
> >> =A0 =A0 =A0print_r($result);
> >>
> >>
> >>
> >>
> >>
> >> This returns:
> >>
> >>
> >>
> >> =A0 =A0 =A0Array
> >>
> >> =A0 =A0 =A0(
> >>
> >> =A0 =A0 =A0 =A0 =A0[1] =3D> 34
> >>
> >> =A0 =A0 =A0 =A0 =A0[4] =3D> 90
> >>
> >> =A0 =A0 =A0)
> >>
> >>
> >>
> >>
> >>
> >> However what I really want is a two-way comparison. =A0I 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. =A0So, 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? =A0Any caveats with that?
> >>
> >>
> >>
> >> =A0 =A0 =A0$array1 =3D array("12", "34", "56", "78", "90");
> >>
> >> =A0 =A0 =A0$array2 =3D array("12", "23", "56", "78", "89");
> >>
> >>
> >>
> >> =A0 =A0 =A0$diff1 =3D array_diff($array1, $array2);
> >>
> >> =A0 =A0 =A0$diff2 =3D array_diff($array2, $array1);
> >>
> >>
> >>
> >> =A0 =A0 =A0$result =3D array_unique(array_merge($diff1, $diff2));
> >>
> >>
> >>
> >> =A0 =A0 =A0print_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 =3D $array(1, 2, 3, 4, 5, 6);
> > $array2 =3D $aray(1, 3, 2, 5);
> >
> > Thanks,
> > Ash
> > http://www.ashleysheridan.co.uk
> >
> >
> >


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Array differences

am 14.04.2010 18:14:42 von lala

Ashley M. Kirchner wrote:
>
> $array1 = array(1, 2, 3, 4, 5, 6);
> $array2 = array(1, 3, 2, 8, 9);
> $diff1 = array_diff($array1, $array2);
> $diff2 = array_diff($array2, $array1);
> $result = array_unique(array_merge($diff1, $diff2));
>
> => (4, 5, 6, 8, 9)

Hi Ash,

Isn't the array_unique() unnecessary?

Mike

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: Array differences

am 14.04.2010 18:20:33 von Ashley

> -----Original Message-----
> From: lala [mailto:lala@mail.theorb.net]
> Sent: Wednesday, April 14, 2010 10:15 AM
> To: Ashley M. Kirchner
> Cc: php-general@lists.php.net
> Subject: Re: [PHP] Array differences
>=20
> Ashley M. Kirchner wrote:
> >
> > $array1 =3D array(1, 2, 3, 4, 5, 6);
> > $array2 =3D array(1, 3, 2, 8, 9);
> > $diff1 =3D array_diff($array1, $array2);
> > $diff2 =3D array_diff($array2, $array1);
> > $result =3D array_unique(array_merge($diff1, $diff2));
> >
> > =3D> (4, 5, 6, 8, 9)
>=20
> Hi Ash,
>=20
> Isn't the array_unique() unnecessary?
>=20
> Mike


Thinking about it, it should be unnecessary, but at the same time I want =
to absolutely sure that I get unique values out of the two diffs.

Ash


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: Array differences

am 14.04.2010 19:20:26 von TedD

At 9:37 AM -0600 4/14/10, Ashley M. Kirchner wrote:
>No because that only does a one-way comparison. It only tells me what's
>missing from $array2. I need it from both arrays. That's why I'm comparing
>1 versus 2, then 2 versus 1, and then doing a merge/unique on the result.
>
>-snip-
> $array1 = array(1, 2, 3, 4, 5, 6);
> $array2 = array(1, 3, 2, 8, 9);
> $diff1 = array_diff($array1, $array2);
> $diff2 = array_diff($array2, $array1);
> $result = array_unique(array_merge($diff1, $diff2));
>
> => (4, 5, 6, 8, 9)
>
>This second $result is what I want. So far I haven't noticed any problems
>doing it this way ... yet. I'm sure someone will tell me otherwise.
>
>Ash

Ash:

Looks good to me. But note the indexes of the result.

You might want to:

$array1 = array(1, 2, 3, 4, 5, 6, 7, 7, 7, 7);
$array2 = array(1, 2, 3, 8, 9);
$diff1 = array_diff($array1, $array2);
$diff2 = array_diff($array2, $array1);

$diff1 = array_unique($diff1);
$diff2 = array_unique($diff2);
$result = array_merge($diff1, $diff2);

Cheers,

tedd

--
-------
http://sperling.com http://ancientstones.com http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php