我尝试过在线搜索解决方案,但没有成功(我是PHP初学者)。我有两个有comma separated values
的strings
(带有时间戳的电话号码集合)。我需要检查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,
下面是一个例子,测试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
我需要做的是COUNT
string A
中逗号之间的值的数量,这些值在string B
中匹配,除了时间戳中的最后5个字符,因为这些值可能是相同的出现,但间隔+/-几秒。
我曾想过使用phpexplode
将每个值放入数组中,然后进行比较,但在这一点上,当涉及到数组以及如何将一个数组值与另一个数组的值减去值的最后5个字符时,我相当困惑。
您可以这样做。
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);
试试这个:
$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.
老实说,我认为将这两个字符串组合起来,解析它们,然后通过多个呼叫查找电话号码更有用。
<?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)
。