查找数组数组中的公共值

发布于 2024-11-28 07:43:05 字数 185 浏览 8 评论 0原文

我有一个数组的数组。

$a = [
    [1, 2, 3],
    [2, 3, 4],
    [2, 4, 5]
];

如何找到所有数组中存在的共同元素?

在上面的示例数据中,我希望有一个包含值 2 的结果数组,因为它是所有行中出现的唯一值。

I have an array of arrays.

$a = [
    [1, 2, 3],
    [2, 3, 4],
    [2, 4, 5]
];

How can I find common element which exist in all array?

In the above sample data, I'd expect to have a result array containing the value 2 because it is the only value that occurs in all rows.

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

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

发布评论

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

评论(4

风铃鹿 2024-12-05 07:43:05

这是我做的一个功能。它只是多维数组的参考。

<?php
$array1 = array('angka'=>12,'satu','2'=>'dua','tiga'=>array('dua','enam','empat'=>array('satu','lima',12)));//object
$array2 = array('dua','tiga','empat',12);//object as intersect refference

function intersect($data=NULL)
{
    if(!empty($data))
    {
        $crashed = array();
        $crashed2 = array();
        foreach($data[0] as $key=>$val)
        {
            if(!is_array($val))
            {
                $crashed[$key] = in_array($val,$data[1]);//return true if crashed (intersect)
            }
            else
            {
                $crashed2[$key] = intersect(array($val,$data[1]));
            }
            $crashed = array_merge($crashed,$crashed2);
        }
    }
    return $crashed;
}
$intersect = intersect(array($array1,$array2));
print_r($intersect);
?>

它返回如下结果:

Array ( [angka] => 1 [0] => [1] => 1 [tiga] => Array ( [0] => 1 [1] => [empat] => Array ( [0] => [1] => [2] => 1 ) ) ) 

如果数组的值与引用数组匹配,则返回 true。

希望代码可以帮到你。

This is a function I have made. It's just a reference for a multidimensional array.

<?php
$array1 = array('angka'=>12,'satu','2'=>'dua','tiga'=>array('dua','enam','empat'=>array('satu','lima',12)));//object
$array2 = array('dua','tiga','empat',12);//object as intersect refference

function intersect($data=NULL)
{
    if(!empty($data))
    {
        $crashed = array();
        $crashed2 = array();
        foreach($data[0] as $key=>$val)
        {
            if(!is_array($val))
            {
                $crashed[$key] = in_array($val,$data[1]);//return true if crashed (intersect)
            }
            else
            {
                $crashed2[$key] = intersect(array($val,$data[1]));
            }
            $crashed = array_merge($crashed,$crashed2);
        }
    }
    return $crashed;
}
$intersect = intersect(array($array1,$array2));
print_r($intersect);
?>

It returns a result like this:

Array ( [angka] => 1 [0] => [1] => 1 [tiga] => Array ( [0] => 1 [1] => [empat] => Array ( [0] => [1] => [2] => 1 ) ) ) 

It returns true if the value of an array matches with a reference array.

Hope the code can help you.

梦初启 2024-12-05 07:43:05

看看这里 array-intersect
你可以这样使用它:

$intersect = $a[0];
for ($i = 1; $i < count($a); $i++)
{
    $intersect = array_intersect($intersect, $a[$i]);
}

Have a look here array-intersect.
You could use it like this:

$intersect = $a[0];
for ($i = 1; $i < count($a); $i++)
{
    $intersect = array_intersect($intersect, $a[$i]);
}
无畏 2024-12-05 07:43:05

您可以通过以下方式避免 foreach 循环

call_user_func_array('array_intersect',$a);

You can avoid foreach loop by

call_user_func_array('array_intersect',$a);
沫尐诺 2024-12-05 07:43:05

将行解压缩到单个 array_intersect() 调用中以确定所有行中出现的值。结果将保留数组第一行中合格值的键。

代码:(Demo)

$array = [
    [1, 2, 3, 4],
    [2, 3, 4, 5],
    [3, 4, 5, 6]
];

var_export(array_intersect(...$array));

输出:

array (
  2 => 3,
  3 => 4,
)

只要第一级键是数字,扩展元素就是一种有效的技术。如果第一级键不是数字,只需在传播之前调用 array_values() 重新索引键即可。

var_export(array_intersect(...array_values($array)));

Unpack the rows into a single array_intersect() call to determine the values which occur in all rows. The result will retain the keys of the qualifying values in the first row of the array.

Code: (Demo)

$array = [
    [1, 2, 3, 4],
    [2, 3, 4, 5],
    [3, 4, 5, 6]
];

var_export(array_intersect(...$array));

Output:

array (
  2 => 3,
  3 => 4,
)

Spreading elements is a valid technique as long as the first level keys are numeric. If the first level keys are not numeric, just call array_values() to re-index the keys before spreading.

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