【问题标题】:How to match letters and check that value is present inside array or not using PHP如何匹配字母并检查数组中是否存在值或不使用PHP
【发布时间】:2017-09-27 07:24:10
【问题描述】:

我需要一个帮助。我需要检查字符串是否存在于数组中,并且它应该使用 PHP 逐字搜索。我在下面解释我的代码。

$resultArr=array("9937229853","9937229856","9937229875");
$searchValue="+919937229853";

这里我需要检查$searchValue 中的一些值是否存在于数组中。我正在像下面那样做,但它没有给我正确的结果。

$searchValue="+919937229853";
$resultArr=array("9937229853","9937229856","9937229875");
if(!in_array($searchValue, $resultArr))
{
 $flag=1;
}else{
  $flag=0;
}
echo $flag;

根据我的要求,这里的结果应该打印1,因为来自$searchValue 的一些值也存在于该数组中,但回显结果即将出现0。请帮助我。

【问题讨论】:

  • 它不存在。 '+919937229853' !== '9937229853'
  • 这就是为什么我说我也需要匹配字符。
  • 即便如此。 +919937229853 不包含在 9937229853 中。如果$searchValue 的一部分在数组中就足够了吗?
  • @subhra,$resultArr 总是只包含数字吗?
  • 是的。它总是只包含数字。

标签: php arrays


【解决方案1】:

你可以试试下面的代码。

if(!in_array(substr($searchValue,-10), $resultArr))

【讨论】:

  • 您确定-10 始终是正确的值吗?
  • 也可能是‘10个或更多’。
【解决方案2】:

下面的函数将返回true

  • 您的 $searchValue 在数组中 (in_array),或者
  • 如果数组中的任何一项是$searchValue的子字符串
    换句话说:如果$searchValue 的一部分在数组中。

这是代码以及你如何称呼它:

function search($needle, $haystack) {
    // is $needle contained in the array as it is?
    if (in_array($needle, $haystack))
        return true;

    // Is any part of $needle part of the array?
    foreach ($haystack as $value) {
        if (strpos($needle, $value) !== FALSE)
            return true;
    }

    return false;
}

$resultArr = array("9937229853", "9937229856", "9937229875");
$searchValue = "+919937229853";

$result = search($searchValue, $resultArr);
var_dump($result);

【讨论】:

  • 总是有结果false
  • 我在末尾添加了var_dump,然后将完整代码复制到phptester.net。它返回true
【解决方案3】:
$searchValue="+919937229853";
$searchValue = str_replace("+91","",$searchValue);
$resultArr=array("9937229853","9937229856","9937229875");
if(in_array($searchValue, $resultArr))
{
 $flag=1;
}else{
  $flag=0;
}
echo $flag;

用户 str_replace 函数替换字符串中的前三个字符

【讨论】:

  • 它可能并不总是+91 它也可能是+911 或其他任何东西。
【解决方案4】:
$flag=0;
for($i=0;$i<strcmp($searchValue);$i++){
    for($j=0;$j<strcmp($searchValue);$j++){
         $part=substr($searchValue,$i,$j)
         array_filter($resultArr, function($el) use ($part) {
             if ( strpos($el, $part) !== false ){
                  $flag=1;
             }
         });
    }
}

【讨论】:

    【解决方案5】:
    $searchValue="+919937229853";
    $resultArr=array("9937229853","9937229856","9937229875");
    
    foreach($resultArr as $value)
    {
        if(strpos($value,$searchValue) || strpos($searchValue,$value) || $searchValue==$value)
        {
            $flag = 1;
            break;
        }
        else
            $flag=0;
    }
    echo $flag;
    
    I think this will do what you are looking for. in_array() method search string in array but for the exact string. strpos can search substring in long string and return the offset number or the index of substring in the long string.
    

    【讨论】:

    • 它适用于这种特殊情况,但如果 $searchValue="9937229853" 它无法按预期工作。
    • 可以添加条件 if(strpos($value,$searchValue) || strpos($searchValue,$value) || $searchValue==$value)
    猜你喜欢
    • 2016-05-10
    • 1970-01-01
    • 1970-01-01
    • 2020-01-05
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    • 2012-04-15
    • 1970-01-01
    相关资源
    最近更新 更多