dpdp42233
2017-11-04 17:26
浏览 131
已采纳

在多维数组中查找匹配项

I have an array that contains phone numbers in different format:

$myArr[0][0] == '122-33-2222';
$myArr[1][0] == '(122) 433-5555';
$myArr[2][0] == '122 644.8888';

I need to check if another number is in that array. I assume I need to loop through array and strip all non-numeric values before I compare.

$findNumber = 122.433.5555;
$varPhone = preg_replace("/[^0-9,.]/", "", $findNumber);

foreach ($myArr AS $phone) {
   if (preg_replace("/[^0-9,.]/", "", $phone) == $varPhone) {
      echo "found";
   } else {
      echo "not found";
   }
}

I think I'm close but it's not quite there. What am I missing?

图片转代码服务由CSDN问答提供 功能建议

我有一个包含不同格式电话号码的数组:

 <  code> $ myArr [0] [0] =='122-33-2222'; 
 $ myArr [1] [0] =='(122)433-5555'; 
 $ myArr [2] [0  ] =='122 644.8888'; 
   
 
 

我需要检查该数组中是否有其他数字。 我假设我需要循环遍历数组并在比较之前去除所有非数字值。

  $ findNumber = 122.433.5555; 
 $ varPhone = preg_replace(“/ [^ 0-9,。] /”,“”,$ findNumber); 
  
 
nforeach($ myArr AS $ phone){
 if(preg_replace(“/ [^ 0-9,。] /”,“”,$ phone)== $ varPhone){
 echo“found”; 
  } else {
 echo“not found”; 
} 
} 
   
 
 

我认为我很接近,但它并不存在。 我错过了什么?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dongshuo6185 2017-11-04 17:36
    已采纳

    The phone number is in the key [0] of each first-level array element, so you can't compare each instance of $phone directly. Also, I would replace all non-digit characters so that different notations still turn out as the same number.

    <?php
    // initialize array for the sake of this demo, to make this snippet work
    $myArr = array(array(), array(), array());
    $myArr[0][0] = '122-33-2222';
    $myArr[1][0] = '(122) 433-5555';
    $myArr[2][0] = '122 644.8888';
    
    $findNumber = "122.433.5555";
    
    function cleanNumber($in) {
      return preg_replace("/[^0-9]/", "", $in);
    }
    
    foreach ($myArr AS $phone) {
       // the number is in the key [0] for each first-level array element
       if (cleanNumber($phone[0]) == cleanNumber($findNumber)) {
          echo "found<br>";
       } else {
          echo "not found<br>";
       }
    }
    

    this will output:

    not found
    found
    not found
    
    点赞 评论
  • dqmhgz5848 2017-11-04 17:36

    There are a few problems with your code, try the following:

    $myArr[0][0] = '122-33-2222';
    $myArr[1][0] = '(122) 433-5555';
    $myArr[2][0] = '122 644.8888';
    
    $findNumber = "122.433.5555";
    
    $varPhone = preg_replace("/[^0-9]/", "", $findNumber);
    
    foreach ($myArr AS $phone)
    {
       $phone = preg_replace("/[^0-9]/", "", $phone);
    
       if ($phone[0] == $varPhone)
       {
            echo "found";
       }
       else
       {
          echo "not found";
       }
    }
    

    Remove the , and . from the regex and as $phone is an array, treat it as such.

    Output:

    not foundfoundnot found
    
    点赞 评论
  • dovhpmnm31216 2017-11-04 17:40

    Please check the following snippet that might work

    <?php
    $myArr[0] = '122-33-2222';
    $myArr[1] = '(122) 433-5555';
    $myArr[2]    = '122 644.8888';
    
    $findNumber = "122.433.5555";
    $varPhone = preg_replace("/[^0-9]/", "", $findNumber);
    $flag = false;
    foreach ($myArr AS $phone) {
       if (preg_replace("/[^0-9]/", "", $phone) == $varPhone) {
          $flag = true;
          break;
    
       } 
    }
    
    if($flag)
        echo "found";
    else
        echo "not found";
    
    ?>
    

    Changes:- $myArr should be 1d array and not 2d array,

    == is comparison operator, assigning operator should be use instead.

    in preg_replace even dots should be replaced with empty

    点赞 评论
  • duanjinchi1982 2017-11-04 17:43

    Here is a working example of your code:

    $myArr[0][0] = '122-33-2222';
    $myArr[1][0] = '(122) 433-5555';
    $myArr[2][0] = '122 644.8888';
    
    $findNumber = '122.433.5555';
    $normalize = preg_replace("/[^0-9]/","", $findNumber);
    
    $found = false;
    foreach ($myArr AS $phone) {
      if ($normalize == preg_replace("/[^0-9]/","", $phone[0])) {
        $found = true;
        break;
      }
    }
    
    echo $found;
    

    An even better approach would be using array_filter

    $myArr[0][0] = '122-33-2222';
    $myArr[1][0] = '(122) 433-5555';
    $myArr[2][0] = '122 644.8888';
    
    $findNumber = '122.433.5555';
    $normalize = preg_replace("/[^0-9]/","", $findNumber);
    
    $filtered =array_filter($myArr, function ($phone) use ($normalize) {
      return preg_replace("/[^0-9]/","", $phone[0]) == $normalize;
    });
    
    var_dump($filtered);
    echo sizeof($filtered);
    
    点赞 评论

相关推荐 更多相似问题