如何比较两个数组中的值?

时间:2021-12-06 08:28:00

It seems that every PHP function I read about for comparing arrays (array_diff(), array_intersect(), etc) compares for the existence of array elements.

似乎我读到的用于比较数组的每个PHP函数(array_diff(),array_intersect()等)都比较了数组元素的存在。

Given two multidimensional arrays with identical structure, how would you list the differences in values?

给定两个具有相同结构的多维数组,您如何列出值的差异?

Example

Array 1

[User1] => Array ([public] => 1
                [private] => 1
                [secret] => 1
               ) 
[User2] => Array ([public] => 1
                [private] => 0
                [secret] => 0
               )

Array 2

[User1] => Array ([public] => 1
                [private] => 0
                [secret] => 1
               ) 
[User2] => Array ([public] => 1
                [private] => 0
                [secret] => 0
               )

Difference

[User1] => Array ([public] => 1
                [private] => 0 //this value is different
                [secret] => 1
               )

So my result would be - "Of all the users, User1 has changed, and the difference is that private is 0 instead of 1."

所以我的结果是 - “在所有用户中,User1已经改变,不同之处在于private是0而不是1。”

4 个解决方案

#1


One way is to write a function to do something similar to this..

一种方法是编写一个函数来做类似的事情。

function compareArray ($array1, $array2)
{
  foreach ($array1 as $key => $value)
  {
    if ($array2[$key] != $value)
    {
      return false;
    }
  }

  return true;
}

You could easily augment that function to return an array of differences in the two..

您可以轻松地扩充该函数以返回两者中的差异数组。

Edit - Here's a refined version that more closely resembles what you're looking for:

编辑 - 这是一个精致的版本,更接近你正在寻找的东西:

function compareArray ($array1, $array2)
{
  var $differences;

  foreach ($array1 as $key => $value)
  {
    if ($array2[$key] != $value)
    {
      $differences[$key][1] = $value;
      $differences[$key][2] = $array2[$key];
    }
  }

  if (sizeof($differences) > 0)
  {
    return $differences;
  }
  else
  { 
    return true;
  }
}

#2


I think this does what you're looking for.

我认为这可以满足您的需求。

Using your sample data, doing a loop on the outer arrays, then using array_diff_assoc on the users each time through. (Note, this assumes that when there's a difference, array_diff_assoc returns the value from the second array passed in, which it seems to do).

使用示例数据,在外部数组上执行循环,然后每次都在用户上使用array_diff_assoc。 (注意,这假设当存在差异时,array_diff_assoc返回传入的第二个数组的值,它似乎这样做)。

<?php
$user1 = array("public" => 1, "private" => 1, "secret" => 1);
$user2 = array("public" => 1, "private" =>1, "secret" => 1);
$array1 = array ("user 1"=>$user1, "user 2"=>$user2);

$user1 = array("public" => 1, "private" => 0, "secret" => 1);
$user2 = array("public" => 1, "private" => 1, "secret" => 1);
$array2 = array("user 1"=>$user1, "user 2"=>$user2);

$results = array();  
foreach ( $array1 as $user => $value )
{
    $diff = array_diff_assoc( $array1[$user], $array2[$user] );
    if ($diff) {
        array_push($results,array($user=>$diff));   
        }
}


print_r($results);


?>

It returns:

Array
(
    [0] => Array
        (
            [user 1] => Array
                (
                    [private] => 1
                )
        )    
)

#3


Try this function:

试试这个功能:

function arrayDiff($array1, $array2)
{
    if (!is_array($array1) || !is_array($array2)) {
        return false;
    }
    foreach ($array1 as $key => $val) {
        if (array_key_exists($key, $array2) && gettype($val) != "array" && $val === $array2[$key]) {
            unset($array1[$key]);
            continue;
        }
        if (is_array($val)) {
            $val = diff($val, $array2[$key]);
            if ($val !== false) {
                $array1[$key] = $val;
            }
        }
    }
    return $array1;
}

$array1 = array(
    array(
        array('foo', 'bar', 'baz'),
        0
    )
);
$array2 = array(
    array(
        array('foo', 'bar')
    )
);
var_dump(diff($array1, $array2));

#4


If you're looking for differences in the values, how about array_diff_assoc. The http://us2.php.net/manual/en/function.array-diff-assoc.php">php manual says it "returns an array containing all the values from array1 that are not present in any of the other arrays" and gives the following example:

如果您正在寻找值的差异,那么array_diff_assoc如何。 http://us2.php.net/manual/en/function.array-diff-assoc.php“> php手册说它”返回一个数组,其中包含array1中任何其他数组中不存在的值“并给出以下示例:

In this example you see the "a" => "green" pair is present in both arrays and thus it is not in the ouput from the function. Unlike this, the pair 0 => "red" is in the ouput because in the second argument "red" has key which is 1

在此示例中,您会看到两个数组中都存在“a”=>“green”对,因此它不在函数的输出中。与此不同,对0 =>“red”在输出中,因为在第二个参数中“red”具有1的键

<?php
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_diff_assoc($array1, $array2);
print_r($result);
?>

The above example will output:

以上示例将输出:

Array
(
    [b] => brown
    [c] => blue
    [0] => red
)

Is this what you're looking for?

这是你在找什么?

#1


One way is to write a function to do something similar to this..

一种方法是编写一个函数来做类似的事情。

function compareArray ($array1, $array2)
{
  foreach ($array1 as $key => $value)
  {
    if ($array2[$key] != $value)
    {
      return false;
    }
  }

  return true;
}

You could easily augment that function to return an array of differences in the two..

您可以轻松地扩充该函数以返回两者中的差异数组。

Edit - Here's a refined version that more closely resembles what you're looking for:

编辑 - 这是一个精致的版本,更接近你正在寻找的东西:

function compareArray ($array1, $array2)
{
  var $differences;

  foreach ($array1 as $key => $value)
  {
    if ($array2[$key] != $value)
    {
      $differences[$key][1] = $value;
      $differences[$key][2] = $array2[$key];
    }
  }

  if (sizeof($differences) > 0)
  {
    return $differences;
  }
  else
  { 
    return true;
  }
}

#2


I think this does what you're looking for.

我认为这可以满足您的需求。

Using your sample data, doing a loop on the outer arrays, then using array_diff_assoc on the users each time through. (Note, this assumes that when there's a difference, array_diff_assoc returns the value from the second array passed in, which it seems to do).

使用示例数据,在外部数组上执行循环,然后每次都在用户上使用array_diff_assoc。 (注意,这假设当存在差异时,array_diff_assoc返回传入的第二个数组的值,它似乎这样做)。

<?php
$user1 = array("public" => 1, "private" => 1, "secret" => 1);
$user2 = array("public" => 1, "private" =>1, "secret" => 1);
$array1 = array ("user 1"=>$user1, "user 2"=>$user2);

$user1 = array("public" => 1, "private" => 0, "secret" => 1);
$user2 = array("public" => 1, "private" => 1, "secret" => 1);
$array2 = array("user 1"=>$user1, "user 2"=>$user2);

$results = array();  
foreach ( $array1 as $user => $value )
{
    $diff = array_diff_assoc( $array1[$user], $array2[$user] );
    if ($diff) {
        array_push($results,array($user=>$diff));   
        }
}


print_r($results);


?>

It returns:

Array
(
    [0] => Array
        (
            [user 1] => Array
                (
                    [private] => 1
                )
        )    
)

#3


Try this function:

试试这个功能:

function arrayDiff($array1, $array2)
{
    if (!is_array($array1) || !is_array($array2)) {
        return false;
    }
    foreach ($array1 as $key => $val) {
        if (array_key_exists($key, $array2) && gettype($val) != "array" && $val === $array2[$key]) {
            unset($array1[$key]);
            continue;
        }
        if (is_array($val)) {
            $val = diff($val, $array2[$key]);
            if ($val !== false) {
                $array1[$key] = $val;
            }
        }
    }
    return $array1;
}

$array1 = array(
    array(
        array('foo', 'bar', 'baz'),
        0
    )
);
$array2 = array(
    array(
        array('foo', 'bar')
    )
);
var_dump(diff($array1, $array2));

#4


If you're looking for differences in the values, how about array_diff_assoc. The http://us2.php.net/manual/en/function.array-diff-assoc.php">php manual says it "returns an array containing all the values from array1 that are not present in any of the other arrays" and gives the following example:

如果您正在寻找值的差异,那么array_diff_assoc如何。 http://us2.php.net/manual/en/function.array-diff-assoc.php“> php手册说它”返回一个数组,其中包含array1中任何其他数组中不存在的值“并给出以下示例:

In this example you see the "a" => "green" pair is present in both arrays and thus it is not in the ouput from the function. Unlike this, the pair 0 => "red" is in the ouput because in the second argument "red" has key which is 1

在此示例中,您会看到两个数组中都存在“a”=>“green”对,因此它不在函数的输出中。与此不同,对0 =>“red”在输出中,因为在第二个参数中“red”具有1的键

<?php
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_diff_assoc($array1, $array2);
print_r($result);
?>

The above example will output:

以上示例将输出:

Array
(
    [b] => brown
    [c] => blue
    [0] => red
)

Is this what you're looking for?

这是你在找什么?