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 1091659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:33:52+00:00 2026-05-16T23:33:52+00:00

given the following arrays in php, how can i best merge them together $masterkeys

  • 0

given the following arrays in php, how can i best merge them together

$masterkeys = array('Key1','Key2');

$settings_foo = array(
     array('ID'=>'Key1',
           'Foo_Setting'=>'SomeValue'));

$settings_bar = array(
     array('ID'=>'Key1',
           'Bar_Setting'=>'SomeOtherValue')); 

in the end I need $masterkeys to be the combination of each of the settings_[foo|bar] arrays.

Array( ['Key1'] =  Array('Foo_Setting'=>'SomeValue','Bar_Setting'=>'SomeOtherValue') );

I do know I can use a couple foreach loops on this, but just wondering if there are a couple PHP array functions that can splice them together.

  • 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-16T23:33:53+00:00Added an answer on May 16, 2026 at 11:33 pm

    While you can use some of PHP’s array functions, your input data isn’t in a very nice format. You’ll have the fewest iterations (and probably best performance) by writing them yourself:

    # create an array of $masterkey => array()
    $result = array_combine($masterkeys, array_fill(0, count($masterkeys), array()));
    
    # loop through each settings array
    foreach (array($settings_foo, $settings_bar) as $settings)
    {
      foreach ($settings as $s)
      {
        # merge the array only if the ID is in the master list
        $key = $s['ID'];
        if (array_key_exists($key, $result))    
          $result[$key] = array_merge($result[$key], $s);
      }
    }
    
    # unset all extraneous 'ID' properties
    foreach (array_keys($result) as $i)
      unset($result[$i]['ID']);
    
    var_dump($result);
    

    As an alternative, you could look into array_map and array_filter, but due to the way the data is structured, I’m not sure they’ll be of much use.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following PHP array: Array ( [0] => Array ( [name] => Restaurant
Given the following php array $a = array( array('a'=>'111','b'=>'two','c'=>'asdasd'), array('a'=>'111','b'=>'one','c'=>'sdvsdfs'), array('a'=>'111','b'=>'three','c'=>'vbndfgn'), array('a'=>'222','b'=>'nine','c'=>'dfhfnd') ); how
I am attempting to create an implode that given the following array: <?php $conditions
I'm simplifying a larger complex problem with the following... Given three arrays of integers,
// given following array: $data = array( 0=>array( data=>object1, col=>array( 0=>array( data=>object2, col=>array( 0=>array(
Given following array: var arr = [undefined, undefined, 2, 5, undefined, undefined]; I'd like
Given the following array: $array = array( 'note' => array('test', 'test1', 'test2', 'test3', 'test4'),
Given the following array a : a = [1, 2, 3, 4, 5] How
Given the following array of json objects: var items = [ {id:1, parentId:0, name:'item1'},
The following returns Warning: range() expects at least 2 parameters, 1 given. Can't figure

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.