在多维数组中查找匹配项

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?

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

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

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

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);