簡體   English   中英

比較兩個數組的值

[英]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');
}

關於如何比較數組以查看是否存在至少一個值的任何想法?

使用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
}

鏈接: http//www.w3schools.com/php/func_array_intersect.asp

使用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');
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM