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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:46:48+00:00 2026-06-16T21:46:48+00:00

How can I merge this three arrays $name ={Tom, John, David}; $v1 = {Tom:100,

  • 0

How can I merge this three arrays

$name ={"Tom", "John", "David"};
$v1 = {"Tom":100, "David":200};
$v2 = {"John":500, "Tom":400};

into one multidimensional associative array in two different ways?
One way is the key order should be same as that of array “name”.

$name_merged_original_order = array (
    "Tom" => Array(
        "v1" => 100,
        "v2" => 400
    ),
    "John" => Array(
        "v1" => "N/A",
        "v2" => 500
    ),
    "David" => Array(
        "v1" => 100,
        "v2" => "N/A"
    )
)

Another ways is the alphabetical of array “name”:

$name_merged_asc = array (
    "David" => Array(
        "v1" => 100,
        "v2" => "N/A"
    ),
    "John" => Array(
        "v1" => "N/A",
        "v2" => 200
    ),
    "Tom" => Array(
        "v1" => 100,
        "v2" => 400
    ),
)

The tricky part is that array “v1” and “v2” is not ordered as the key of “name.” They also don’t have all keys as in “name.” Thanks!

  • 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-16T21:46:50+00:00Added an answer on June 16, 2026 at 9:46 pm

    It’s not tested and the easiest solution:

    $name_merged_original_order = array();
    foreach($name as $key){
        $name_merged_original_order[$key] = array();
        if(array_key_exists($key, $v1)){
            $name_merged_original_order[$key]['v1'] = $v1[$key];
        }
        else{
            $name_merged_original_order[$key]['v1'] = 'N/A';
        }
        if(array_key_exists($key, $v2)){
            $name_merged_original_order[$key]['v2'] = $v2[$key];
        }
        else{
            $name_merged_original_order[$key]['v2'] = 'N/A';
        }
    }
    
    sort($name);
    $name_merged_asc = array();
    foreach($name as $key){
        $name_merged_asc[$key] = array();
        if(array_key_exists($key, $v1)){
            $name_merged_asc[$key]['v1'] = $v1[$key];
        }
        else{
            $name_merged_asc[$key]['v1'] = 'N/A';
        }
        if(array_key_exists($key, $v2)){
            $name_merged_asc[$key]['v2'] = $v2[$key];
        }
        else{
            $name_merged_asc[$key]['v2'] = 'N/A';
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can I merge two arrays in JavaScript like this? these arrays: arr1 = ['one','two','three'];
I have this simple example I can't seems to get working : MERGE INTO
This is a follow-on question from this one: Algorithm to dynamically merge arrays There
Currently, I can add the creator_id like this in my controller: @entry = Entry.new(params[:entry].merge(:creator
EntityManager.merge() can insert new objects and update existing ones. Why would one want to
var _sel1 = $('.red'); var _sel2 = $('.yellow'); How can I merge them into
How can you get the number of affected rows from executing a MERGE INTO...
what would be the fastest way to merge a list of numpy arrays into
I'm trying to merge to sorted arrays into a third sorted array , but
I have three arrays: htext[i], ptext[i], and ytext[i] I was wondering how I can

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.