Jump to content


Photo

array difference


  • Please log in to reply
4 replies to this topic

#1 wolves

wolves
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 20 July 2006 - 05:58 PM

first look at my arrays
//my first array
Array
(
    [key1] => Array
        (
            [0] => Value1
            [1] => Value2
            [2] => Value3
            [3] => Value4
            [4] => Value5
            [5] => Value6
            [6] => Value7
            [7] => Value8
            [8] => Value9
        )

    [key2] => Array
        (
            [0] => Value1
            [1] => Value2
            [2] => Value3
        )

    [key3] => Array
        (
            [0] => Value2
            [1] => Value2
        )

)

//my second array

Array
(
    [key1] => Array
        ( 
            [6] => Value7
            [7] => Value8
            [8] => Value9
        )

    [key2] => Array
        (
            [0] => Value1
        )

    [key3] => Array
        (
            [0] => Value2
        )

)

Now there is a way to get an array that is the result of array1 less array2? and other that is what array2 have that array1 doesn't have, using native php functions?

tks

#2 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 20 July 2006 - 06:11 PM

array_diff()
array_intersect()

Check the manual for details.

#3 wolves

wolves
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 20 July 2006 - 06:28 PM

I'm trying to use this functions but no functional results....

array_diff($array1, $array2) returns empty array

array_diff($array2, $array1) returns empty array

array_intersect($array1, $array2) returns array2

array_intersect($array2, $array1) return array1

don't know how to get array1 - array2...

#4 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 20 July 2006 - 07:48 PM

you should loop through array1 and use array_diff on the second level arrays. the reason why you got empty array from array_diff is that this funciton is not recursive.

foreach($array1 as $key=>$arr)
{
   $result[$key] = array_diff($arr,$array2[$key]);
}

Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#5 wolves

wolves
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 20 July 2006 - 08:19 PM

tks, it's solved my problemn




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users