...

View Full Version : very strange problem with array_diff()



Bobafart
03-02-2008, 11:21 PM
very strange problem with array_diff()

the problem: watch what happens to "aaaaaaaaaaaaaa" which exists in my first array but doesn't in my second -- array_diff() doesn't keep it (neither does array_intersect) -- it is lost!



// my first array: $groupTagsArray

array(5) { [0]=> string(8) "religion" [1]=> string(6) "christ" [2]=> string(6) "muslim" [3]=> string(6) "jewish" [4]=> string(14) "aaaaaaaaaaaaaa" }

// my second array: $GetOriginalTagsArray

array(10) { [0]=> string(3) "sss" [1]=> string(3) "sss" [2]=> string(3) "aaa" [3]=> string(2) "kj" [4]=> string(2) "ss" [5]=> string(3) "sss" [6]=> string(8) "religion" [7]=> string(6) "christ" [8]=> string(6) "muslim" [9]=> string(6) "jewish" }

// my array_diff($GetOriginalTagsArray, $groupTagsArray)

array(6) { [0]=> string(3) "sss" [1]=> string(3) "sss" [2]=> string(3) "aaa" [3]=> string(2) "kj" [4]=> string(2) "ss" [5]=> string(3) "sss" }


why isn't "aaaaaaaaaaaaaa" in my array_diff()?

Bobafart
03-02-2008, 11:28 PM
if i sort() the arrays I get this:




$groupTagsArray:

array(5) { [0]=> string(14) "aaaaaaaaaaaaaa" [1]=> string(6) "christ" [2]=> string(6) "jewish" [3]=> string(6) "muslim" [4]=> string(8) "religion" }



$GetOriginalTagsArray:

array(10) { [0]=> string(3) "aaa" [1]=> string(6) "christ" [2]=> string(6) "jewish" [3]=> string(2) "kj" [4]=> string(6) "muslim" [5]=> string(8) "religion" [6]=> string(2) "ss" [7]=> string(3) "sss" [8]=> string(3) "sss" [9]=> string(3) "sss" }



array_diff($GetOriginalTagsArray, $groupTagsArray):

array(6) { [0]=> string(3) "aaa" [3]=> string(2) "kj" [6]=> string(2) "ss" [7]=> string(3) "sss" [8]=> string(3) "sss" [9]=> string(3) "sss" }



and for the fun of it, in case you were wondering:




array_intersect($GetOriginalTagsArray, $groupTagsArray):

array(4) { [1]=> string(6) "christ" [2]=> string(6) "jewish" [4]=> string(6) "muslim" [5]=> string(8) "religion" }

Bobafart
03-02-2008, 11:34 PM
even better, I reversed the order of the arrays in array_intersect and array_diff, this is what I get





array_intersect($groupTagsArray, $GetOriginalTagsArray, ):

array(4) { [1]=> string(6) "christ" [2]=> string(6) "jewish" [3]=> string(6) "muslim" [4]=> string(8) "religion" }

array_diff($groupTagsArray, $GetOriginalTagsArray, ):

array(1) { [0]=> string(12) "aaaaaaaaaaaa" }




it would seem that array_diff and array_intersect stop when they find equal elements in arrays -- is it supposed to work this way?

what a pain in the ***.. they are useless functions if this is the case.

anyone have any bright ideas on how I can create two arrays - one with the like elements and one with the different elements?

_Aerospace_Eng_
03-03-2008, 12:11 AM
Hmm maybe array_intersect_assoc is what you need or array_diff_assoc.

Bobafart
03-03-2008, 02:05 AM
ok will try those

oesxyl
03-03-2008, 05:04 AM
very strange problem with array_diff()

the problem: watch what happens to "aaaaaaaaaaaaaa" which exists in my first array but doesn't in my second -- array_diff() doesn't keep it (neither does array_intersect) -- it is lost!


array_diff compute the difference between $GetOriginalTagsArray and $groupTagsArray, that means the result will be all the elements from $GetOriginalTagsArray which are not in $groupTagsArray and "aaaaaaaaaaaaaa" is not in $GetOriginalTagsArray.
The difference is not the same if change the order of arguments:



array_diff($groupTagsArray, $GetOriginalTagsArray)


array_intersect works too, because it compute only the elements which are in both arrays.



// $A and $B to array
// compute common elements of both:
$I = array_intersect($A,$B);
// compute the elements that differ, :)
$D = array_diff(array_merge($A,$B),$I);


not tested


- now is tested and work.



best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum