2016-06-17 8 views
1

all!2つの値がphpの配列にあるかどうかを確認してください

だから、私はこのコードを持っている:私はこの

in_array($clienthwid and $clientip, $client_whitelist) 

私はどのようにしているかどうかを確認しますを行う方法を知りたいのです

in_array($clienthwid, $client_whitelist); 

のために、そう

<?php 
$clienthwid = $_POST["clienthwid"]; 
$clientip = $_POST["clientip"]; 
$hwid_logs = "hwid_log.txt"; 
$ip_logs = "ip_log.txt"; 
$handle_ip = fopen($ip_logs, 'a') or die("404 file not found"); 
$handle_hwid = fopen($hwid_logs, 'a') or die("404 file not found"); 
$client_whitelist = array (
    // put hwids and ip here 
    "hwid" => "123456789", "12345678", 
    "ip" => "123.456.789", "123.456.788", 
); 
//check if client hwid or ip is in the array 
if (in_array($clienthwid, $client_whitelist)) { 
    echo "TRUE"; 
    fwrite($handle_hwid, $clienthwid."\n"); 
    fwrite($handle_ip, $clientip."\n"); 
} else { 
    echo "FALSE"; 
    fwrite($handle_hwid, $clienthwid."\n"); 
    fwrite($handle_ip, $clientip."\n"); 
} 
?> 

を2つの変数が配列にありますか?

+0

クライアントホワイトリストの配列は正しい形式ですか? hwidとipの値をグループ化するためのサブ配列を持たないはずですか? – Progrock

+0

[this](http://stackoverflow.com/a/7542708/5447994) – Thamilan

+0

の可能な複製[in \ _array multiple values]の可能な複製(http://stackoverflow.com/questions/7542694/in-array- –

答えて

2

2つのin_array文を使用してください。

in_array($clienthwid, $client_whitelist) && in_array($clientip, $client_whitelist) 

両方$client_whitelistである場合、これが唯一の真のだろう。 少なくともオンがあるかどうかを確認する場合は、||または演算子を使用します。

//check if client hwid or ip is in the array 
if (in_array($clienthwid, $client_whitelist) || in_array($clientip, $client_whitelist)) { 
    echo "TRUE"; 
    fwrite($handle_hwid, $clienthwid."\n"); 
    fwrite($handle_ip, $clientip."\n"); 
} else { 
    echo "FALSE"; 
    fwrite($handle_hwid, $clienthwid."\n"); 
    fwrite($handle_ip, $clientip."\n"); 
} 
+0

ありがとう、私はPHPに新しいのです! – Ryan

+0

あなたを歓迎します!私たちはどこかで始めました:) – cb0

0

参照の

$array = array("AA","BB","CC","DD"); 
$check1 = array("AA","CC"); 
$check2 = array("EE","FF"); 

if(array_intersect($array, $check1)) { 
    echo "Exist"; 
}else{ 
    echo "Not exist"; 
} 

if(array_intersect($array, $check2)) { 
    echo "Exist"; 
}else{ 
    echo "Not exist"; 
} 

array_intersect使用:すべての指定されたアイテムがターゲット配列にある場合3v4l

0

機能ベローズがチェックされます。それはforeachを使い、かなり簡単です。

if (multiple_in_array(array('onething', 'anotherthing'), $array)) { 
    // 'onething' and 'anotherthing' are in array 
} 

function multiple_in_array($needles = array(), $haystack = array()) 
{ 
    foreach ($needles as $needle) { 
     if (!in_array($needle, $haystack)) { 
      return false; 
     } 
    } 
    return true; 
} 
関連する問題