通过比较行来获取两个多维数组之间的差异

发布于 2024-11-08 02:07:12 字数 761 浏览 1 评论 0原文

我有两个嵌套数组,需要找到引用数组和数据数组之间的差异。我正在使用 array_dif_assoc 函数,并且无法获得正确的差异,我不知道为什么我无法获得它。如果我犯了一些错误或者我必须递归地做,有人可以帮助我吗?

$allCoursesAvailable = array(array('id'=>0,'name'=>'Select-One'), array('id'=>1,'name'=>'course1'),array('id'=>1,'name'=>'course2'),array('id'=>3,'name'=>'course3'));

$allCoursesforUser = array(array('id'=>0,'name'=>'Select-One'), array('id'=>1,'name'=>'course1'),array('id'=>4,'name'=>'course4'),array('id'=>5,'name'=>'course5'),array('id'=>6,'name'=>'course4'));

echo '<pre>';print_r(array_diff_assoc($allCoursesAvailable,$allCoursesforUser));

我得到一个空数组。当我使用 array_diff_assoc() 时,我应该获得携带 course2 和 course3 的数组,因为它们不是第二个数组的一部分。

我是否缺少 PHP 端的一些逻辑?

I have two nested arrays and need to find the difference between the reference array and the data array. I am using array_dif_assoc function, and am unable to get the right difference, I am not sure why I am unable to get it. Could somebody help me out if I am doing some mistake or if I have to do it recursively;

$allCoursesAvailable = array(array('id'=>0,'name'=>'Select-One'), array('id'=>1,'name'=>'course1'),array('id'=>1,'name'=>'course2'),array('id'=>3,'name'=>'course3'));

$allCoursesforUser = array(array('id'=>0,'name'=>'Select-One'), array('id'=>1,'name'=>'course1'),array('id'=>4,'name'=>'course4'),array('id'=>5,'name'=>'course5'),array('id'=>6,'name'=>'course4'));

echo '<pre>';print_r(array_diff_assoc($allCoursesAvailable,$allCoursesforUser));

I am getting an empty array with this. When I use array_diff_assoc(), I should have got the arrays carrying course2 and course3, as they are not part of the 2nd array.

Am I missing some logic on the PHP end?

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

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

发布评论

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

评论(2

谜泪 2024-11-15 02:07:12

您始终可以从阅读 PHP 手册开始。

对于array_diff_assoc(在http://php. net/manual/en/function.array-diff-assoc.php)据说此函数仅检查 n 维数组的一维。当然,您可以使用例如 array_diff_assoc($array1[0], $array2[0]); 来检查更深的维度。

在用户评论中提供了解决方案(这确实有效):

imars dot com 的 55 点 php
2009 年 3 月 17 日 03:09 我曾经工作过
提到 array_diff_assoc_recursive()
作者:chinello,gmail dot com 和我
认为这可能值得一提
这里。我写了十几个测试用例
看起来效果很好。

<?php
// dwarven Differences:
// * Replaced isset() with array_key_exists() to account for keys with null contents

// 55 dot php at imars dot com Differences:
// Key differences:
// * Removed redundant test;
// * Returns false bool on exact match (not zero integer);
// * Use type-precise comparison "!==" instead of loose "!=";
// * Detect when $array2 contains extraneous elements;
// * Returns "before" and "after" instead of only "before" arrays on mismatch.

function array_compare($array1, $array2) {
    $diff = false;
    // Left-to-right
    foreach ($array1 as $key => $value) {
        if (!array_key_exists($key,$array2)) {
            $diff[0][$key] = $value;
        } elseif (is_array($value)) {
             if (!is_array($array2[$key])) {
                    $diff[0][$key] = $value;
                    $diff[1][$key] = $array2[$key];
             } else {
                    $new = array_compare($value, $array2[$key]);
                    if ($new !== false) {
                         if (isset($new[0])) $diff[0][$key] = $new[0];
                         if (isset($new[1])) $diff[1][$key] = $new[1];
                    };
             };
        } elseif ($array2[$key] !== $value) {
             $diff[0][$key] = $value;
             $diff[1][$key] = $array2[$key];
        };
 };
 // Right-to-left
 foreach ($array2 as $key => $value) {
        if (!array_key_exists($key,$array1)) {
             $diff[1][$key] = $value;
        };
        // No direct comparsion because matching keys were compared in the
        // left-to-right loop earlier, recursively.
 };
 return $diff;
};
?>

You could always start by reading the PHP manual.

For array_diff_assoc (on http://php.net/manual/en/function.array-diff-assoc.php) it is said that this function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using, for example, array_diff_assoc($array1[0], $array2[0]);.

Provided solution in user comments (this does work):

55 dot php at imars dot com
17-Mar-2009 03:09 I've worked on
array_diff_assoc_recursive() mentioned
by chinello at gmail dot com and I
think it might be worth mentioning
here. I wrote a dozen test cases and
it seems to be holding up pretty well.

<?php
// dwarven Differences:
// * Replaced isset() with array_key_exists() to account for keys with null contents

// 55 dot php at imars dot com Differences:
// Key differences:
// * Removed redundant test;
// * Returns false bool on exact match (not zero integer);
// * Use type-precise comparison "!==" instead of loose "!=";
// * Detect when $array2 contains extraneous elements;
// * Returns "before" and "after" instead of only "before" arrays on mismatch.

function array_compare($array1, $array2) {
    $diff = false;
    // Left-to-right
    foreach ($array1 as $key => $value) {
        if (!array_key_exists($key,$array2)) {
            $diff[0][$key] = $value;
        } elseif (is_array($value)) {
             if (!is_array($array2[$key])) {
                    $diff[0][$key] = $value;
                    $diff[1][$key] = $array2[$key];
             } else {
                    $new = array_compare($value, $array2[$key]);
                    if ($new !== false) {
                         if (isset($new[0])) $diff[0][$key] = $new[0];
                         if (isset($new[1])) $diff[1][$key] = $new[1];
                    };
             };
        } elseif ($array2[$key] !== $value) {
             $diff[0][$key] = $value;
             $diff[1][$key] = $array2[$key];
        };
 };
 // Right-to-left
 foreach ($array2 as $key => $value) {
        if (!array_key_exists($key,$array1)) {
             $diff[1][$key] = $value;
        };
        // No direct comparsion because matching keys were compared in the
        // left-to-right loop earlier, recursively.
 };
 return $diff;
};
?>
老娘不死你永远是小三 2024-11-15 02:07:12

您不应该使用 _assoc 而应使用普通的 array_diff 或 < a href="http://php.net/array_intersect" rel="nofollow">array_intersect,因为否则它将比较外部数组的顺序,而不仅仅是内容。

另外 array_diff 并不真正适用于子数组,您需要一个解决方法:

print_r(
  array_map("unserialize",
    array_diff(
      array_map("serialize", $allCoursesAvailable),
      array_map("serialize", $allCoursesforUser)
    )
  )
);

这会给您:

[2] => Array
    (
        [id] => 1
        [name] => course2
    )

[3] => Array
    (
        [id] => 3
        [name] => course3
    )

不确定这是否是您想要的。手动执行此操作可能仍然是可取的。

You should not use _assoc but the normal array_diff or array_intersect, because otherwise it will compare the ordering of the outer array and not just the content.

Also array_diff doesn't really work with subarrays, and you'd need a workaround:

print_r(
  array_map("unserialize",
    array_diff(
      array_map("serialize", $allCoursesAvailable),
      array_map("serialize", $allCoursesforUser)
    )
  )
);

Which would give you:

[2] => Array
    (
        [id] => 1
        [name] => course2
    )

[3] => Array
    (
        [id] => 3
        [name] => course3
    )

Not sure if that is what you wanted. And doing it manually might be advisable still.

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