문제

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 ?

도움이 되었습니까?

해결책

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;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top