如何比较两个二维数组中的行?

发布于 2024-07-13 06:25:55 字数 975 浏览 5 评论 0原文

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

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

示例

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
               )

所以我的结果是 - “在所有用户中,User1 已更改,区别在于 private 是 0 而不是 1。”

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

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."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心如狂蝶 2024-07-20 06:25:55

一种方法是编写一个函数来执行与此类似的操作。

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

  return true;
}

您可以轻松地扩展该函数以返回两者之间的差异数组。

编辑-这是一个更完善的版本,它更接近您正在寻找的内容:

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;
  }
}

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;
  }
}
谜兔 2024-07-20 06:25:55

我认为这正是您正在寻找的。

使用示例数据,在外部数组上执行循环,然后每次都对用户使用 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);


?>

它返回:

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

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).

<?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
                )
        )    
)
心安伴我暖 2024-07-20 06:25:55

试试这个功能:

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));

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));
2024-07-20 06:25:55

如果您正在寻找值的差异,array_diff_assoc 怎么样。 php 手册 说它“返回一个包含array1 中不存在于任何其他数组中的所有”并给出以下示例:

在此示例中,您会看到“a”=>;
“绿色”对存在于两个数组中
因此它不在输出中
功能。 与此不同的是,对 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);
?>

上面的例子将输出:

<前><代码>数组

[b] => 棕色的
[c] => 蓝色的
[0] => 红色的

这是你要找的吗?

If you're looking for differences in the values, how about array_diff_assoc. The 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:

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

<?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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文