我有一个包含3个数组的数组。
我必须合并它但是如果三个数组中的任何一个的值与任何其他两个数组的值匹配,则键应该是总和。
即。 [bing] [10]与[google] [10]匹配(url实际上没有清理,我的错误)和[yahoo] [10] 所以新的合并数组应该在第一个位置有[url] [30],依此类推。
我怎样才能做到这一点?
我的阵列:http://pastebin.com/tSfrCcMJ
$array = array (
'bing' => array (
10 => 'http://stackoverflow.com/',
9 => 'http://www.stackoverflow.es/',
8 => 'http://stackoverflow.com/questions',
7 => 'http://www.stackoverflow.es/empresa/avisolegal',
6 => 'http://stackoverflow.net/',
5 => 'http://chat.stackoverflow.com/',
4 => 'http://blog.stackoverflow.com/',
3 => 'http://chat.stackoverflow.com/?tab=all&sort=active&page=16',
2 => 'http://meta.stackoverflow.com/',
1 => 'http://careers.stackoverflow.com/ewernli'
),
'google' => array (
10 => 'http://stackoverflow.com/&sa=U&ei=oMg7T_rpJ4rz-gazm_SsBw&ved=0CBYQFjAA&usg=AFQjCNFOHjfhg0MrXOGxhxoLkWY6BP7Erw',
9 => 'http://stackoverflow.com/users/login',
8 => 'http://en.wikipedia.org/wiki/Stack_overflow&sa=U&ei=oMg7T_rpJ4rz-gazm_SsBw&ved=0CC0QFjAH&usg=AFQjCNFaLvYDIANOTluG7kTQZppgPK1OuQ',
7 => 'http://blog.stackexchange.com/&sa=U&ei=oMg7T_rpJ4rz-gazm_SsBw&ved=0CDAQFjAI&usg=AFQjCNFM47UgedUUcCIIENkkEpGT1F5-VQ',
6 => 'http://itc.conversationsnetwork.org/series/stackoverflow.html&sa=U&ei=oMg7T_rpJ4rz-gazm_SsBw&ved=0CDMQFjAJ&usg=AFQjCNEhtBxP6KPK9A2IIHzjqGETn5kVgA',
5 => 'http://stackoverflow.org/&sa=U&ei=oMg7T_rpJ4rz-gazm_SsBw&ved=0CDYQFjAK&usg=AFQjCNFsYAEUQYofh1C2k0IfppDSwwxAUA',
4 => 'http://stackoverflow.net/&sa=U&ei=oMg7T_rpJ4rz-gazm_SsBw&ved=0CDgQFjAL&usg=AFQjCNH55YZyZeh8q75--kYkyCg8nRuf4g',
3 => 'http://www.crunchbase.com/company/stack-exchange&sa=U&ei=oMg7T_rpJ4rz-gazm_SsBw&ved=0CDsQFjAM&usg=AFQjCNETf6XyPdfFqJC5-6F5NFxGjDY2wA',
2 => 'http://embeddedgurus.com/stack-overflow/&sa=U&ei=oMg7T_rpJ4rz-gazm_SsBw&ved=0CEAQFjAN&usg=AFQjCNE-vRAAhmbu_OzwpI6EoI-9va12LA',
1 => 'http://www.haskell.org/haskellwiki/Stack_overflow&sa=U&ei=oMg7T_rpJ4rz-gazm_SsBw&ved=0CEMQFjAO&usg=AFQjCNEhsp34I-FC-dW0fG0-ZogG7T-qXg',
0 => 'http://highscalability.com/blog/2011/3/3/stack-overflow-architecture-update-now-at-95-million-page-vi.html&sa=U&ei=oMg7T_rpJ4rz-gazm_SsBw&ved=0CEYQFjAP&usg=AFQjCNEf7K09RvPYSDxWKKhDdCpDj1hs1w'
),
'yahoo' => array (
10 => 'http://stackoverflow.com/',
9 => 'http://en.wikipedia.org/wiki/Stack_overflow',
8 => 'http://stackoverflow.com/about',
7 => 'http://en.wikipedia.org/wiki/Stackoverflow',
6 => 'http://blog.stackoverflow.com/',
5 => 'http://facebook.stackoverflow.com/',
4 => 'http://stackoverflow.com/questions',
3 => 'http://stackoverflow.net/',
2 => 'http://stackoverflow.com/faq',
1 => 'http://stackoverflow.com/questions/ask'
)
);
期望的结果(我只做'http://stackoverflow.com/'匹配,键总和):
krsort($array);
$result = array ( 30 => 'http://stackoverflow.com/', 9 => 'http://en.wikipedia.org/wiki/Stack_overflow', 8 => 'http://stackoverflow.com/about', 7 => 'http://en.wikipedia.org/wiki/Stackoverflow', 6 => 'http://blog.stackoverflow.com/', 5 => 'http://facebook.stackoverflow.com/', 4 => 'http://stackoverflow.com/questions', 3 => 'http://stackoverflow.net/', 2 => 'http://stackoverflow.com/faq', 1 => 'http://stackoverflow.com/questions/ask', 0 => 'http://highscalability.com/blog/2011/3/3/stack-overflow-architecture-update-now-at-95-million-page-vi.html&sa=U&ei=oMg7T_rpJ4rz-gazm_SsBw&ved=0CEYQFjAP&usg=AFQjCNEf7K09RvPYSDxWKKhDdCpDj1hs1w', );
答案 0 :(得分:1)
您无法获得密钥的分数,因为当两个条目具有相同分数时,您可能会覆盖值。你能做的是:
$urls = array();
array_walk_recursive($array, function ($url, $score) use (&$urls) {
$key = strtok($url, '&');
$urls[$key] = isset($urls[$key]) ? $urls[$key] + $score : $score;
});
arsort($urls);
print_r($urls);
给出
Array
(
[http://stackoverflow.com/] => 30
[http://en.wikipedia.org/wiki/Stack_overflow] => 17
[http://stackoverflow.net/] => 13
[http://stackoverflow.com/questions] => 12
[http://blog.stackoverflow.com/] => 10
[http://stackoverflow.com/users/login] => 9
…