检查php

时间:2016-06-17 10:07:00

标签: php arrays

所有!

所以,我有这段代码:

<?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");
}
?>

所以,对于

in_array($clienthwid, $client_whitelist);

我想知道如何做到这一点

in_array($clienthwid and $clientip, $client_whitelist)

如何检查数组中是否有两个变量?

3 个答案:

答案 0 :(得分:2)

只需使用两个in_array语句。

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

仅当两者都在$client_whitelist时才会出现这种情况。 如果您想确定是否至少有on,请使用||或运算符。

//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");
}

答案 1 :(得分:0)

使用array_intersect

$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"; 
}
  

参考:3v4l

答案 2 :(得分:0)

下面的函数将检查所有给定项是否都在目标数组中。它使用了foreach并且非常简单。

row = 1 1 1 2 3 3
col = 1 3 4 3 2 5