php-如何从关联数组中剔除类似项?
更新:有人错误地编辑了我的问题,但现在已修复 假设我有以下关联数组:php-如何从关联数组中剔除类似项?,php,Php,更新:有人错误地编辑了我的问题,但现在已修复 假设我有以下关联数组: Array ( [Postponement no issue for Man Utd (BBC) ] => 8 [Postponement no issue for Man Utd ] => 7 [Postponement no issue for Man Utd: Manchester United say they have no issue over Sunday's game at Chelsea b
Array (
[Postponement no issue for Man Utd (BBC) ] => 8
[Postponement no issue for Man Utd ] => 7
[Postponement no issue for Man Utd: Manchester United say they have no issue over Sunday's game at Chelsea being ... ] => 3
)
你会注意到,曼恩Utd的“推迟无问题”一词出现在所有三个关键点中。我想将此阵列转换为如下所示的新阵列:
Array ( [Postponement no issue for Man Utd] => 18 )
function compareKeys($val1,$val2) {
return (strpos($val1,$val2)!==false);
}
其中18是键包含Man Utd延迟无问题的所有值之和
换句话说:如果一个键出现在另一个键中,则后者将被丢弃到新数组中,其值将添加到前一个键的值上
这可能吗?如何实现?提前谢谢
foreach($array as $k1 => $v1)
foreach($array as $k2 => $v2)
if(strpos($k2, $k1) !== false) // or === 0
$result[$k1] = (isset($result[$k1]) ? $result[$k1] : 0) + $v2;
未经测试的方法可以是:
$original_array = array(...); // The original array with keys and values
asort($original_array); // Sort the array by its keys
$clean_array = array();
foreach ($original_array as $key => $value)
{
foreach (array_keys($clean_array) as $clean_key)
{
$found = false;
if (preg_match('/'.preg_quote($clean_key).'/', $key))
{
$clean_array[$clean_key] += $value;
$found = true;
break;
}
if (!$found) $clean_array[$key] = $value;
}
}
好的。。。执行此操作的唯一方法是在数组中迭代两次,并使用函数比较它们:
foreach ($my_array as $key1 => $value1) {
foreach ($my_array as $key2 => $value2) {
if ($key1 != $key2) { // don't compare them if they're the same
if (compareKeys($key1,$key2)) {
$my_array[$key1] += $value2;
unset($my_array[$key2]);
}
}
}
}
然后,您的compareKeys函数可以如下所示:
Array ( [Postponement no issue for Man Utd] => 18 )
function compareKeys($val1,$val2) {
return (strpos($val1,$val2)!==false);
}
根据数据的大小,这可能不可行。几乎可以肯定有一个更为优化的解决方案,但这是可行的:
<?php
$array = array(
'Postponement no issue for Man Utd (BBC)' => 8,
'Postponement no issue for Man Utd' => 7,
'Postponement no issue for Man Utd: Manchester United say they have no issue over Sunday\'s game at Chelsea being ...' => 3
);
$keys = array_keys($array);
usort($keys, 'lengthCmp');
$flippedKeys = array_flip($keys);
$data = array();
foreach ($keys as $k => $v)
{
$sum = 0;
foreach ($array as $key => $val)
{
if (stripos($key, $v) !== FALSE)
{
$sum += $val;
unset($array[$key]);
unset($keys[$flippedKeys[$v]]);
}
}
$data[$v] = $sum;
}
foreach ($data as $key => $val)
{
if ($val == 0)
unset($data[$key]);
}
var_dump($data);
function lengthCmp($a, $b)
{
return strlen($a) - strlen($b);
}
?>
使用此数据集:
$array = array(
'test test' => 1,
'Postponement no issue for Man Utd (BBC)' => 8,
'Postponement no issue for Man Utd' => 7,
'Postponement no issue for Man Utd: Manchester United say they have no issue over Sunday\'s game at Chelsea being ...' => 3,
'test' => 1,
'not here' => 1,
'another test' => 1
);
输出:
array(1) {["Postponement no issue for Man Utd"] => int(18)}
array(3) {
["test"] => int(3)
["not here"] => int(1)
["Postponement no issue for Man Utd"] => int(18)
}
难道P组不需要通过P发布和延期吗?你是否已经知道你想要所有类似“延期对曼联没有问题”的钥匙,或者需要在飞行中确定吗?@jasonbar需要在飞行中确定是否始终必须至少有一个键完全匹配,或者您是否可以定义有意义的匹配所需的最小字符串长度,以避免仅匹配P,例如?是文字/边界匹配还是字符的任意组合?匹配必须出现在一个键的开头还是它的任何地方?我很好奇你的数组来自哪里。如果是一些数据库,也许你可以做一些相关匹配。这给了我以下信息:数组[推迟曼联没有问题BBC]=>15[推迟曼联没有问题:曼联说他们周日对切尔西的比赛没有问题…]=>3…也无法解释