从多维数组中提取值

发布于 2024-09-13 22:50:30 字数 1090 浏览 3 评论 0原文

我有两个像这样结构的数组

$array1 = Array
 (
     [0] => Array
         (
             ['story_id'] => 47789
         )

     [1] => Array
         (
             ['story_id'] => 47779
         )

     [2] => Array
         (
             ['story_id'] => 47776
         )

     [3] => Array
         (
             ['story_id'] => 47773
         )

     [4] => Array
         (
             ['story_id'] => 47763
         )
 )


$array2 = Array
 (
     [0] => Array
         (
             ['story_id'] => 47789
         )

     [1] => Array
         (
             ['story_id'] => 47777
         )

     [2] => Array
         (
             ['story_id'] => 47776
         )

     [3] => Array
         (
             ['story_id'] => 47773
         )

     [4] => Array
         (
             ['story_id'] => 47763
         )
 )

,我想得到 array1 和 array2 的区别,所以我尝试使用

    $results = array_diff($array1, $array2);

,但结果是空的,有没有简单的方法可以解决这个问题,或者最好让我将数组归结为如果是这样,有没有简单的方法可以做到这一点?

I have two arrays that are structured like this

$array1 = Array
 (
     [0] => Array
         (
             ['story_id'] => 47789
         )

     [1] => Array
         (
             ['story_id'] => 47779
         )

     [2] => Array
         (
             ['story_id'] => 47776
         )

     [3] => Array
         (
             ['story_id'] => 47773
         )

     [4] => Array
         (
             ['story_id'] => 47763
         )
 )


$array2 = Array
 (
     [0] => Array
         (
             ['story_id'] => 47789
         )

     [1] => Array
         (
             ['story_id'] => 47777
         )

     [2] => Array
         (
             ['story_id'] => 47776
         )

     [3] => Array
         (
             ['story_id'] => 47773
         )

     [4] => Array
         (
             ['story_id'] => 47763
         )
 )

and I want to get the difference of array1 from array2 so I tried using

    $results = array_diff($array1, $array2);

but it turns up empty is there any easy way around this or would it be best for me to get the arrays boiled down and if so is there easy way to do that ?

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

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

发布评论

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

评论(1

自我难过 2024-09-20 22:50:30

这是因为 array_diff 仅用于一维数组。对于你的 2 数组,让我们使用 php.net 中的一些代码

function multidimensional_array_diff($a1, $a2)
{
$r = array();

foreach ($a2 as $key => $second) {
    foreach ($a1 as $key => $first) {

        if (isset($a2[$key])) {
            foreach ($first as $first_value) {
                foreach ($second as $second_value) {
                    if ($first_value == $second_value) {
                        $true = true;
                        break;
                    }
                }
                if (!isset($true)) {

                    $r[$key][] = $first_value;
                }
                unset($true);
            }
        } else {
            $r[$key] = $first;
        }
    }
}
return $r;
}

It because that the array_diff is only use for 1 dimension array. For your 2 array, let use some code from php.net

function multidimensional_array_diff($a1, $a2)
{
$r = array();

foreach ($a2 as $key => $second) {
    foreach ($a1 as $key => $first) {

        if (isset($a2[$key])) {
            foreach ($first as $first_value) {
                foreach ($second as $second_value) {
                    if ($first_value == $second_value) {
                        $true = true;
                        break;
                    }
                }
                if (!isset($true)) {

                    $r[$key][] = $first_value;
                }
                unset($true);
            }
        } else {
            $r[$key] = $first;
        }
    }
}
return $r;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文