PHP比较数组并返回不同的数据

时间:2015-07-28 07:07:33

标签: php arrays

我有两个包含数据的数组,我需要比较两个并返回不匹配的数组。

我有两个看起来像这样的数组:

$arr1 = array(
           array('name' => 'Alan', 'age' => '34', 'country' => 'usa'),
           array('name' => 'James', 'age' => '24', 'country' => 'spain' ),
           );

$arr2 = array(
           array('name' => 'Alan', 'age' => '34', 'country' => 'usa'),
           array('name' => 'James', 'age' => '54', 'country' => 'spffain' ),
        );

我想按名称,年龄和国家比较数组,并返回不匹配的数组。

到目前为止我的代码:

$intersect = array_uintersect($arr1, $arr2, 'compareDeepValue');
echo "<pre>", print_r($intersect);

function compareDeepValue($val1, $val2)
{
    return strcmp($val1['age'], $val2['age']);
    return strcmp($val1['country'], $val2['country']);
    return strcmp($val1['name'], $val2['name']);

}

上面的代码返回匹配的数组。我怎样才能更改代码以获得不匹配的数组?

预期输出:

Array
(
    [0] => Array
        (
            [name] => James
            [age] => 21
            [country] => spain
        )

)

5 个答案:

答案 0 :(得分:3)

Your Arrays:

$arr1 = array(
           array('name' => 'Alan', 'age' => '34', 'country' => 'usa'),
           array('name' => 'James', 'age' => '24', 'country' => 'spain' ),
           );

$arr2 = array(
           array('name' => 'Alan', 'age' => '34', 'country' => 'usa'),
           array('name' => 'James', 'age' => '54', 'country' => 'spffain' ),
        );



foreach($arr1 as $key=>$arr)
{
 $bool = true; 
 $ar1 = $arr;
 $ar2 = $arr2[$key];

 foreach($ar1 as $ky=>$val)
{
  if($val != $ar2[$ky])
  {
    $bool = false;
    break;
  }
}

 if(!$bool)
{
  echo "Unmatched Arrays: \r\n";
  print_r($ar1); echo " in  Main Array 1 &  \r\n";
  print_r($ar2); echo " in  Main Array 2. \r\n";
}

}

答案 1 :(得分:3)

某人在答案中提到的代码会起作用,但这是手工劳动:) 您可以使用现有功能为您完成工作。要计算数组之间的差异,您应该使用array_udiff函数(或相关函数)。

您应该编写比较数组的函数,并使用它来计算差异,如下所示:

<?php
$arr1 = array(
       array('name' => 'Alan', 'age' => '34', 'country' => 'usa'),
       array('name' => 'James', 'age' => '24', 'country' => 'spain' ),
       );

$arr2 = array(
       array('name' => 'Alan', 'age' => '34', 'country' => 'usa'),
       array('name' => 'James', 'age' => '54', 'country' => 'spffain' ),
    );

// this function checks if 2 arrays with keys and scalar values are the same
function array_same_check($deepArr1, $deepArr2) {
   //check if arrays are the same
   $diffArr = array_diff_assoc($deepArr1, $deepArr2);
   //if $diffArr has 0 elements - arrays are the same
   if (count($diffArr) === 0) {
      return 0;
   }
   // arrays are not the same - return arbitratry 1 or -1
   return 1;
}

// now let's compare $arr1 and $arr2 you have 
// (meaning: compare the difference between arrays containing arrays) - we use function above
$differing = array_udiff ($arr1, $arr2, 'array_same_check');

print_r($differing);

我将此代码复制到PHPFiddle,它似乎按预期工作。

答案 2 :(得分:2)

尝试这个

$arr1 = array(
       array('name' => 'Alan', 'age' => '34', 'country' => 'usa'),
       array('name' => 'James', 'age' => '24', 'country' => 'spain' ),
       );

$arr2 = array(
       array('name' => 'Alan', 'age' => '34', 'country' => 'usa'),
       array('name' => 'James', 'age' => '54', 'country' => 'spffain' ),
    );


$tmpArray = array();

 foreach($arr1 as $data1) {

  $duplicate = false;
   foreach($arr2 as $data2) {
    if($data1['name'] === $data2['name'] && $data1['age'] === $data2['age'] && $data1['country'] === $data2['country']) $duplicate = true;
    }

 if($duplicate === false) $tmpArray[] = $data1;
 }
 echo "<pre>", print_r($tmpArray);

答案 3 :(得分:2)

$intersect = array_uintersect($arr1, $arr2, 'compareDeepValue');
print_r($intersect);
// add this and it will return the missing array.
print_r(array_diff_key($arr1, $intersect));

function compareDeepValue($val1, $val2)
{
    return strcmp($val1['age'], $val2['age']);
    return strcmp($val1['country'], $val2['country']);
    return strcmp($val1['name'], $val2['name']);
}

答案 4 :(得分:0)

我想再给你一个可能的解决方案。我已经检查了代码,它似乎符合您的需求。

    /** @var array $arr1 */
    $arr1 = array(
            array(
                    'name'    => 'Alan',
                    'age'     => '34',
                    'country' => 'usa'
            ),
            array(
                    'name'    => 'James',
                    'age'     => '24',
                    'country' => 'spain'
            ),
    );

    /** @var array $arr2 */
    $arr2 = array(
            array(
                    'name'    => 'Alan',
                    'age'     => '34',
                    'country' => 'usa'
            ),
            array(
                    'name'    => 'James',
                    'age'     => '54',
                    'country' => 'spffain'
            ),
    );

    /** @var array $notMatched */
    $notMatched = array();
    /**
     * @var int   $index
     * @var array $element
     */
    foreach($arr1 as $index => $element) {
        /**
         * @var string $key
         * @var string $value
         */
        foreach($element as $key => $value) {
            if($arr2[$index][$key] !== $value) {
                $notMatched["arr1"] = $arr1[$index];
                $notMatched["arr2"] = $arr2[$index];
            }
        }
    }
    if(!empty($notMatched)) {
        echo "Unmatched arrays: \r\n";
        echo "Array 1:\r\n";
        print_r($notMatched["arr1"]);
        echo "Array 2:\r\n";
        print_r($notMatched["arr2"]);
    }