更新:有人错误地编辑了我的问题,但现在已修复
假设我有以下关联数组:
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
)
你会注意到所有三个键都出现了“Man Utd的推迟没有问题”这个词。我想把这个数组变成一个像这样的新数组:
Array ( [Postponement no issue for Man Utd] => 18 )
其中18是所有值的总和,其中键包含“Man Utd的延期无问题”。
换句话说:如果一个键出现在另一个键中,后者将从新数组中删除,并将其值添加到前一个键的值上。
这可能吗?怎么样?提前谢谢。
答案 0 :(得分:0)
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;
未测试
答案 1 :(得分:0)
一个简单的方法可能是:
$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;
}
}
答案 2 :(得分:0)
好的......这样做的唯一方法是迭代数组两次,并有一个比较它们的函数:
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函数看起来像这样:
function compareKeys($val1,$val2) {
return (strpos($val1,$val2)!==false);
}
答案 3 :(得分:0)
根据您的数据大小,这可能不可行。几乎肯定有一个更优化的解决方案,但这有效:
<?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(1) {["Postponement no issue for Man Utd"] => int(18)}
使用此数据集:
$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(3) {
["test"] => int(3)
["not here"] => int(1)
["Postponement no issue for Man Utd"] => int(18)
}