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

  • Home
  • SEARCH
  • 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 7969767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:17:19+00:00 2026-06-04T07:17:19+00:00

I have 2 arrays that I want to merge based on a key’s value

  • 0

I have 2 arrays that I want to merge based on a key’s value in array 1. In the below example, I want the game_modes put into the games_list based on a key (id) in games_list.

Array 1 which is pulled from a table full of games:

$games_list = array(
  0 => array(
    'id' => 23,
    'name' => 'Call of Duty: Modern Warfare 3'
    ),
  2 => array(
    'id' => 1,
    'name' => 'Call of Duty: Black Ops'
    )
  );

Array 2 which is pulled from a table full of game modes:

$game_modes = array(
  0 => array(
    'id' => 1,
    'game_id' => 1,
    'description' => 'Capture the Flag'
    ),
  1 => array(
    'id' => 2,
    'game_id' => 1,
    'description => 'Domination'
    ),

  2 => array(
    'id' => 3,
    'game_id' => 23,
    'description' => 'Kill Confirmed'
    )
  );

I would like the result to be:

$games_list = array(
  0 => array(
    'id' => 23,
    'name' => 'Call of Duty: Modern Warfare 3'
    'modes' => array(
        array(
          'id' => 3,
          'game_id' => 23,
          'description' => 'Kill Confirmed'
          )
        )
    ),
  2 => array(
    'id' => 1,
    'name' => 'Call of Duty: Black Ops'
    'modes'=> array(
      0 => array(
        'id' => 1,
        'game_id' => 1,
        'description' => 'Capture the Flag'
        ),
      1 => array(
        'id' => 2,
        'game_id' => 1,
        'description => 'Domination'
        )
      )
    )
  );

Additional info, the site I’m working on currently has 71 games in its database and each game could have some arbitrary number of game modes.

Now, I could easily do a bunch of for loops and avoid this question all together. As of right now I don’t have ton of game modes entered into the database but I keep adding more all the time. Over time doing exponentially more loops all the time will eventually make the page load speed come to a crawl.

I have taken the time to place this data into memcache to make future calls quicker avoiding the loop.

I’ve never been good with array_map as I don’t quite understand how it works or if its even the right route.

  • 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-06-04T07:17:20+00:00Added an answer on June 4, 2026 at 7:17 am

    Don’t you think query level solution would be better?
    The lengthy way would be:

    // array:  $game_modes;
    // array:  $game_lists;
    
    foreach ($game_modes as $gm=>$modes){
       if (isset($modes['game_id'])){
          foreach ($game_lists as $gl=>$lists){
             if ($lists['id'] == $modes['game_id']){
                $game_lists[$gl]['modes'][] = $modes;
                //break;
             }
          }
       }
    }
    

    Output Category : Summary

    $query = 'SELECT 
                    g.id, g.name_name,
                    group_concat(gm.description) as descriptions
              FROM games as g
              LEFT JOIN games_modes as gm
                   ON g.id = gm.game_id
              GROUP BY g.id';
    

    Result:

      id |  name                     |  descriptions
    ------------------------------------------------------------
      1  |  Call of Duty: Black Ops  |  Capture the Flag, Domination
    

    Output Category : Detail

    $query = 'SELECT 
                    g.id, g.name_name,
                    gm.id, gm.description
              FROM games as g
              LEFT JOIN games_modes as gm
                   ON g.id = gm.game_id
              ORDER BY g.id';
    

    Result:

      id |  name                     |  id   |  description
    ----- --------------------------- ------- ------------------
      1  |  Call of Duty: Black Ops  |   1   |  Capture the Flag
      1  |  Call of Duty: Black Ops  |   2   |  Domination
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array that I want to sort based on the value of
I have 2 same-length arrays and I want to merge them into first array
I have one array. I want that array to retain its value between function
I have 2 arrays of objects, I want to merge them such that the
I have 2 arrays $arr1 = array(1,3); $arr2 = array(2,4); I want merge them
I want to have two arrays: supplierList - that contains the int ID of
I have following two arrays. I want the difference between these two arrays. That
I have a deque that contains std::arrays . I want to convert it to
I have an array that I want on multiple pages, so I made it
I have an associative array that I want to display using TileList. However, it

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.