比较PHP三个数组的值?

I have a maybe stupid question?

I have three arrays. And I want to get different values from the first and third array. I created the following code but the returned values are wrong.

function ec($str){
    echo $str.'<br>';
}

$arr1 = array( array(
                    'letter' => 'A',
                    'number' => '1'
                ),

               array(
                    'letter' => 'B',
                    'number' => '2'
                ),

               array(
                    'letter' => 'C',
                    'number' => '3'
                )

    );

$arr2 = array( array(
                    'letter' => 'A',
                    'number' => '1'
                ),

               array(
                    'letter' => 'B',
                    'number' => '2'
                )

    );



 $arr3 = array( array(
                    'letter' => 'D',
                    'number' => '4'
                ),

               array(
                    'letter' => 'E',
                    'number' => '5'
                )

    );

    $mergeArr = array_merge($arr1,$arr3);
    foreach ($mergeArr as $kMerge => $vMerge){
        foreach ($arr2 as $val2){
            if($val2['letter'] != $mergeArr[$kMerge]['letter']){
                ec($mergeArr[$kMerge]['letter']);
            }
        }
    }

The result of this code is:

A
B
C
C
D
D
E
E

The result I want:

    C
    D
    E

Thanks in advance.

Based on the result you are looking for, this should do it:

$mergeArr = array_merge($arr1,$arr3);

$res = array_diff_assoc($mergeArr, $arr2);

var_dump($res);

See the snippet on codepad.

Try this instead of your foreach's:

 $diff = array_diff($mergeArr, $arr2);

 foreach( $diff as $d_k => $d_v ) {
   ec($d_v['letter']);
 }

If I understand what you are trying to do correctly, this function should do the job:

function find_unique_entries () {
  $found = $repeated = array();
  $args = func_get_args();
  $key = array_shift($args);
  foreach ($args as $arg) {
    if (!is_array($arg)) return FALSE; // all arguments muct be arrays
    foreach ($arg as $inner) {
      if (!isset($inner[$key])) continue; 
      if (!in_array($inner[$key], $found)) {
        $found[] = $inner[$key];
      } else {
        $repeated[] = $inner[$key];
      }
    }
  }
  return array_diff($found, $repeated);
}

Pass the key you are searching to the first arguments, then as many arrays as you like in the subsequent arguments. Returns an array of results or FALSE on error.

So your usage line would be:

$result = find_unique_entries('letter', $arr1, $arr2, $arr3);

See it working