Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7503683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:18:58+00:00 2026-05-29T21:18:58+00:00

I have an array with 3 arrays inside. I have to merge it BUT

  • 0

I have an array with 3 arrays inside.

I have to merge it BUT if a value of any of the three arrays matchs with the value of any of the other two arrays, the keys should be sum.

I.E.
[bing][10] matchs with [google][10] (url not cleaned actually, my mistake) and [yahoo][10]
so the new merged array should have [url][30] in the first position, and so on.

how can I achieve this?

My array: 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'
    )
);

Desired result (I only did the ‘http://stackoverflow.com/’ match, keys sum):

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', );
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T21:18:59+00:00Added an answer on May 29, 2026 at 9:18 pm

    You cannot have the score for keys because you risk overwriting values when two entries have the same score. What you can do is:

    $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);
    

    gives

    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
        …
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a nested array grouping three other arrays: $online, $busy and $offline, in
I have an array composed by other array: example my array with two arrays
I have an array that has some other arrays inside it: if I print_r($u)
I have an array that contains an array of arrays if that makes any
I have two arrays: Array ( [2005] => 0 [2006] => 0 [2007] =>
let's say I have two arrays like so: $array1 = array('A' => array( 'B'
I have populated a tableview with an array of arrays, but I do not
I have dictionary with arrays inside: dicarr = {'category': ['post1','post2', 'e.g.'], 'category2': ['post1','post2']} Array
I have three big header files (4.5MB and more), with huge static arrays inside.
I have two arrays, The $first has 5000 arrays inside it and looks like:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.