数组检查数据不完整

时间:2017-04-08 00:22:10

标签: php arrays if-statement

我试图检查agianst数组,我的代码是以下

$coax = array("86.52", "85.218", "178.155", "212.10", "212.112", "62.107", "5.206");


    if(in_array("86.52.16.14", $coax))
            {
              echo "jubii";
                   } else
                   {
                   echo "nej nej nej";
                   }
        }

但我的问题是我必须在数组中检查xx.xx.xx.xx与xx.xx。那有什么工作吗?

2 个答案:

答案 0 :(得分:0)

循环遍历数组,测试输入IP是否以数组元素开头。

$found = false;
foreach ($coax as $x) {
    if (strpos("86.52.16.14", $x) === 0) {
        $found = true;
        break;
    }
}

if ($found) {
    echo "jubii";
} else {
    echo "nej nej nej";
}

答案 1 :(得分:-1)

你可以爆炸出每个数组的前两个部分,然后像这样检查同轴数据:

$coax = array("86.52", "85.218", "178.155", "212.10", "212.112", "62.107", "5.206");
$checkValue = "86.52.16.14";
$tempArray = explode(".",$checkValue);
$checkValueFormatted = $tempArray[0] .".".$tempArray[1];

if(in_array($checkValueFormatted, $coax))
            {
              echo "jubii";
                   } else
                   {
                   echo "nej nej nej";
                   }
        }
}

如果你让$ checkValue成为一个数组,你可以在其上输入一个foreach并一次检查多个值。你也可以扔一个foreach,这样你可以更快地检查多个值......

    $coax = array("86.52", "85.218", "178.155", "212.10", "212.112", "62.107", "5.206");
    $checkValues = array("86.52.16.14", "42.12.1231.1231", "212.10.123.123");

    foreach ($checkValues as $value) {

    $tempArray = explode(".",$value);
    $checkValueFormatted = $tempArray[0] .".".$tempArray[1];
    if(in_array($checkValueFormatted, $coax))
                {
                  echo "jubii";
                       } else
                       {
                       echo "nej nej nej";
                       }
            }
    }
}