Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Dec 2006
    Posts
    417
    Thanks
    168
    Thanked 1 Time in 1 Post

    very strange problem with array_diff()

    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!

    Code:
    // 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()?

  • #2
    Regular Coder
    Join Date
    Dec 2006
    Posts
    417
    Thanks
    168
    Thanked 1 Time in 1 Post
    if i sort() the arrays I get this:

    Code:
    $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:

    Code:
    array_intersect($GetOriginalTagsArray, $groupTagsArray):
    
    array(4) { [1]=>  string(6) "christ" [2]=>  string(6) "jewish" [4]=>  string(6) "muslim" [5]=>  string(8) "religion" }

  • #3
    Regular Coder
    Join Date
    Dec 2006
    Posts
    417
    Thanks
    168
    Thanked 1 Time in 1 Post
    even better, I reversed the order of the arrays in array_intersect and array_diff, this is what I get


    Code:
    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?

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Hmm maybe array_intersect_assoc is what you need or array_diff_assoc.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #5
    Regular Coder
    Join Date
    Dec 2006
    Posts
    417
    Thanks
    168
    Thanked 1 Time in 1 Post
    ok will try those

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Bobafart View Post
    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:

    Code:
    array_diff($groupTagsArray, $GetOriginalTagsArray)
    array_intersect works too, because it compute only the elements which are in both arrays.

    PHP Code:
    // $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

    Edit:
    - now is tested and work.



    best regards
    Last edited by oesxyl; 03-03-2008 at 06:29 AM.

  • Users who have thanked oesxyl for this post:

    Bobafart (03-03-2008)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •