views:

36

answers:

1

I have two arrays that are structured like this

$array1 = Array
 (
     [0] => Array
         (
             ['story_id'] => 47789
         )

     [1] => Array
         (
             ['story_id'] => 47779
         )

     [2] => Array
         (
             ['story_id'] => 47776
         )

     [3] => Array
         (
             ['story_id'] => 47773
         )

     [4] => Array
         (
             ['story_id'] => 47763
         )
 )


$array2 = Array
 (
     [0] => Array
         (
             ['story_id'] => 47789
         )

     [1] => Array
         (
             ['story_id'] => 47777
         )

     [2] => Array
         (
             ['story_id'] => 47776
         )

     [3] => Array
         (
             ['story_id'] => 47773
         )

     [4] => Array
         (
             ['story_id'] => 47763
         )
 )

and I want to get the difference of array1 from array2 so I tried using

    $results = array_diff($array1, $array2);

but it turns up empty is there any easy way around this or would it be best for me to get the arrays boiled down and if so is there easy way to do that ?

A: 

It because that the array_diff is only use for 1 dimension array. For your 2 array, let use some code from php.net

function multidimensional_array_diff($a1, $a2)
{
$r = array();

foreach ($a2 as $key => $second) {
    foreach ($a1 as $key => $first) {

        if (isset($a2[$key])) {
            foreach ($first as $first_value) {
                foreach ($second as $second_value) {
                    if ($first_value == $second_value) {
                        $true = true;
                        break;
                    }
                }
                if (!isset($true)) {

                    $r[$key][] = $first_value;
                }
                unset($true);
            }
        } else {
            $r[$key] = $first;
        }
    }
}
return $r;
}
Bang Dao
I'm sure that would do the trick but its more code than I am willing to deal with for this instance what I ended up doing was looping through them to flatten then used diff
mcgrailm