1

我试过在线搜索解决方案,但没有运气(我是 PHP 初学者)。我有两个strings(带有时间戳的电话号码集合)具有comma separated values. 我需要检查逗号之间的特定文本部分是否sting A可以在string B.

以下是刺痛 A 的示例:

9858264-2012-12-05T00:11:28.806Z,1265482-2012-12-05T22:19:49.769Z,9598643-2012-12-05T22:46:17.115Z,

这是 og sting B 的示例:

5555649-2012-12-05T22:37:23.765Z,3594595-2012-12-05T22:44:36.363Z,8549851-2012-12-05T22:46:01.259Z,9598643-2012-12-05T22:46:09.600Z


在上述字符串中,以下两个值非常相似(时间戳中只有几秒不同):

From string A: 9598643-2012-12-05T22:46:17.115Z
From string B: 9598643-2012-12-05T22:46:09.600Z


我需要做的是除了时间戳中的最后 5 个字符之外的匹配中COUNT逗号之间的值的数量,因为这些可能是相同的事件,但相隔 +/- 几秒钟。string Astring B


我曾想过使用 phpexplode将每个值放入一个数组中,然后将它们进行比较,但是当涉及到数组以及如何将一个数组值与另一个减去最后 5 个字符进行比较时,我相当迷茫一个值。

4

3 回答 3

1

你可以做这样的事情。

function make_string_into_nice_array ($string) {
    $string_array = explode(',',$string);
    return array_map(function($str) {
        return substr($str, 0, -5);    
    }, $string_array);
}

$array_a = make_string_into_nice_array($string_a);
$array_b = make_string_into_nice_array($string_b);

$matched_array = array_intersect($array_a, $array_b);

echo count($matched_array);
于 2012-12-05T23:41:49.857 回答
1

试试这个:

$arrayA = explode(',', $StringA); // Parse the string into array elements, using comma as delimiter.
$arrayB = explode(',', $StringB);

$matches = array(); // Declare the array that will be the output.
foreach ($arrayA as $aValue) { // Iterate through each element of A.
    foreach ($arrayB as $bValue) { // Iterate through each element of B.
        // Take the value of element A (chopping off the last 5 characters) and compare with the value of element B (chopping off the last 5 characters)
        if (substr($aValue, 0, strlen($aValue)-5) == substr($bValue, 0, strlen($bValue)-5))) {
            // If there's a match, iterate the count of that particular key in your output array.
            $matches[substr($aValue, 0, strlen($aValue)-5)] += 1;
        }
    }
}
echo count($matches); // Print the number of keys that are duped.
print_r($matches); // Look at each individual key and the number of duplicated instances.
于 2012-12-05T23:45:50.617 回答
1

老实说,我认为将两个字符串组合起来,解析它们,然后查找不止一个电话的电话号码会更有用。

<?php
$input =
   trim(
      '9858264-2012-12-05T00:11:28.806Z,1265482-2012-12-05T22:19:49.769Z,9598643-2012-12-05T22:46:17.115Z,' .
      '5555649-2012-12-05T22:37:23.765Z,3594595-2012-12-05T22:44:36.363Z,8549851-2012-12-05T22:46:01.259Z,9598643-2012-12-05T22:46:09.600Z',
      ','
   ); //gets rid of leading/trailing commas
$raw_arr = explode(',', $input);

$phone_records = array();

foreach( $raw_arr as $entry ) {
   $temp = explode('T', $entry);
   $time = $temp[1];
   $temp = explode('-', $temp[0]);
   $phone = array_shift($temp);
   $date = implode('-', $temp);

   if( ! isset($phone_records[$phone]) ) {
      $phone_records[$phone] = array();
   }
   $phone_records[$phone][] = array($date, $time);
}

//print_r($phone_records);

foreach( $phone_records as $number => $entries ) {
   if( count($entries) > 1 ) {
      printf('%s: %s', $number, var_export($entries, TRUE));
   }
}

输出:

9598643: array (
  0 =>
  array (
    0 => '2012-12-05',
    1 => '22:46:17.115Z',
  ),
  1 =>
  array (
    0 => '2012-12-05',
    1 => '22:46:09.600Z',
  ),
)

您还可以使用此代码将每个输入解析为自己的数组,然后使用 foreach 循环运行一个并检查值是否为array_key_exists($arr1_key, $arr2).

于 2012-12-05T23:48:29.153 回答