Jump to content

Archived

This topic is now archived and is closed to further replies.

wolves

array difference

Recommended Posts

first look at my arrays
[code]
//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
        )

)
[/code]

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

Share this post


Link to post
Share on other sites
array_diff()
array_intersect()

Check the manual for details.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.