比较两个数组的值
Compare two arrays for a value
我有两个数组,其中包含一些定义用户是否有权访问这篇文章的数据。文章将被标记为客户端,即 ClientA、ClientB 和用户在创建时将被分配一个客户端访问标签。我想比较两个数组,如果它们至少有一个,我会给它们访问权限,如果没有,它们将被重定向。
数组结构如下:
array(1) {
[0] "ClientA"
}
array(3) {
[0] "ClientA"
[1] "ClientB"
[2] "ClientC"
}
我曾尝试使用 in_array,但返回的结果为 false,例如
//$articleClient is the array with one value and $client is the
//array with 3 values
if (!in_array($articleClient, $client)) {
dd('no access');
}
关于如何比较数组以查看是否存在至少一个值的任何想法?
使用array_intersect()
函数
$result = array_intersect($array1, $array2);
if(sizeof($result)>0)
{
//match
}else
{
//no match
}
试试这个
$common = array_intersect($articleClient, $client)
if (count($common) < 1) {
dd('no access');
}
在php中使用函数array_intersect
函数。例如:
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");
$result=array_intersect($a1,$a2)
if (count($result)>=1)
{
//give access to the user
}
我有两个数组,其中包含一些定义用户是否有权访问这篇文章的数据。文章将被标记为客户端,即 ClientA、ClientB 和用户在创建时将被分配一个客户端访问标签。我想比较两个数组,如果它们至少有一个,我会给它们访问权限,如果没有,它们将被重定向。
数组结构如下:
array(1) {
[0] "ClientA"
}
array(3) {
[0] "ClientA"
[1] "ClientB"
[2] "ClientC"
}
我曾尝试使用 in_array,但返回的结果为 false,例如
//$articleClient is the array with one value and $client is the
//array with 3 values
if (!in_array($articleClient, $client)) {
dd('no access');
}
关于如何比较数组以查看是否存在至少一个值的任何想法?
使用array_intersect()
函数
$result = array_intersect($array1, $array2);
if(sizeof($result)>0)
{
//match
}else
{
//no match
}
试试这个
$common = array_intersect($articleClient, $client)
if (count($common) < 1) {
dd('no access');
}
在php中使用函数array_intersect
函数。例如:
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");
$result=array_intersect($a1,$a2)
if (count($result)>=1)
{
//give access to the user
}